Sign in to follow this  
Followers 0
iagman

Dollar Formatting

9 posts in this topic

My problem is formatting data into a dollar output. Ive tried the StringFormat examples shown on this board as well as inside the AutoIt Help, but cant get it to work.

This formatting is what I need:

If the calculated data is 100, then I want to display it as 100.00.

If the calculated data is 100.1, then I want to show 100.10.

All the calculated data is numerical.

Not having any success with StringFormat, I am now trying my own approach but, it too, doesnt give the correct results. Can someone spot what is wrong with code below?

My first step is to change numberical variable ($fi) into a string ($fi_string), then with StringInStr search for decimal location (if there is one) and format output accordingly:

Func FormatOutput($PAD)
    $fi_string = String($fi)
    
    $DecPos_fi = StringInStr ($fi_string, ".", 0, -1) ; begin searching from right side
    
        If $DecPos_fi = 0 Then
            $MakeCurrencyFormat = ".00"
        ElseIf $DecPos_fi = 1 Then
            $MakeCurrencyFormat = "00"
        ElseIf $DecPos_fi = 2 Then
            $MakeCurrencyFormat = "0"
            ElseIf $DecPos_fi <2 Then
            $MakeCurrencyFormat = ""
        Else
            EndIf
    Return($PAD) 
EndFunc

After deterimining $MakeCurrencyFormat, I then concantenate that with this statement further down in code.

GUICtrlSetData ( 31, " " & $fi & $MakeCurrencyFormat)

My thought was that the correct amount of zeros (or none) would be added to produce the dollar format.

The problem seems to be that the StringInStr does not locate the decimal because when I insert a messagebox, I can see that the $DecPos value is incorrect. If the calculated value of $fi is 100.1 (decimal is position two counting from right) then $DecPos shows 0.

Any assistance appreciated.

iagman

Share this post


Link to post
Share on other sites



With StringFormat, it would work this way:

$Value = InputBox("Number", "number")

$Value = Round($Value,2) ; Round to 2 decimal places
$result = StringFormat("%#.2f",$Value) ; Format to exactly 2 decimal places

MsgBox(0, '', $result)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

With StringFormat, it would work this way:

$Value = InputBox("Number", "number")

$Value = Round($Value,2) ; Round to 2 decimal places
$result = StringFormat("%#.2f",$Value) ; Format to exactly 2 decimal places

MsgBox(0, '', $result)
You replied while I was still testing it but I don't think you even need the Round() in there.

;
$aDoll = StringSplit("100|100.1|100.10|150", "|")
For $i = 1 To Ubound($aDoll) -1
   $aDoll[$i] = StringFormat("%#.2f", $aDoll[$i])
   MsgBox(0, "Test", $aDoll[$i])
Next
;

@iagman

I would also suggest that you add the code to add the thousands separator when working with dollar values

;
#include<string.au3>
$aDoll = StringSplit("100|100.1|100.10|15050", "|")
For $i = 1 To Ubound($aDoll) -1
   $aDoll[$i] = StringFormat("%#.2f", $aDoll[$i])
   $aDoll[$i] = _StringAddThousandsSep($aDoll[$i]);; uses localized settings
   MsgBox(0, "Test", $aDoll[$i])
Next
;
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

HI, You can also use one of these:

$Amt=582948.43
$GCF=DllCall("kernel32.dll","int","GetCurrencyFormat","int",0,"int",0,"str",$Amt, _
                            "int",0,"str",0,"int",20)
MsgBox(0,$Amt,$GCF[5])

; Or this one
$VBS=ObjCreate("MSScriptControl.ScriptControl")
$VBS.Language="VBScript"
$D=$VBS.Eval("FormatCurrency("&$Amt&")")
MsgBox(0,"VBS ScriptControl",$D)

$VBS=0

Share this post


Link to post
Share on other sites

Couple of points just to show how confused a newcomer can become even when reading instruction from individuals that know their code.

First,

$Amt=582948.43

$GCF=DllCall("kernel32.dll","int","GetCurrencyFormat","int",0,"int",0,"str",$Amt, _

"int",0,"str",0,"int",20)

MsgBox(0,$Amt,$GCF[5])

_______________________________

Second,

$VBS=ObjCreate("MSScriptControl.ScriptControl")

$VBS.Language="VBScript"

$D=$VBS.Eval("FormatCurrency("&$Amt&")")

MsgBox(0,"VBS ScriptControl",$D)

The first section of the above code I had previously found at this forum and had tried to use it. But it is totally unclear how this is set up so I could never get it to properly format for dollars. I assumed I had only to substitute my variable in place of the $Amt and leave everything else the same. If so, that produced no dollar formatting results.

In the second section of the code above, does that require the user to have a DLL file already installed in order to run Visual Basic?

Now, for the StringFormat examples you gave. You create an input box using:

$Value = InputBox("Number", "number")

I understand that places a string value into $Value.

I need to use several inputs to compute multiple final values. It is these values that I need to format to dollars. If my inputs are made with strings, then are they converted to numbers before doing calculations? If so, what would be the advantage to that over the method below.

My code for receiving user input is:

$Input_2 = GuiCtrlCreateInput("0",110, 63, 80, 20)

This places a number value into $Input_2. With this and other entered numbers I compute the final values needed. Then, I covert those to strings so formatting can take place with string functions.

$fi_string = String($fi) ; the conversion

After the conversion I apply your example:

StringFormat ("%#.2f", $fi_string) ; Format to currency

With the numbers I am using as a test, I get an output of 368.8, not 368.80. This is the same thing I was getting previously.

The example of StringSplit Ive not yet tried so I cant comment. It looks interesting and I will try to understand how it works before tying it out.

An additional question resulted from this code in one of your examples:

$Value = InputBox("Number", "number")

$Value = Round($Value,2) ; Round to 2 decimal places

It looks to me that you are rounding the string in $Value. Can you round a string like it was a number?

By the way, I should have mentioned in my initial post that all my values have been rounded to two decimal places prior to converting them into strings.

Most problems stem from inexperience. Just yesterday I realized ELSE didnt work if it was followed by addition code on the same line as can be done with THEN. That one took me about an hour of frustration to figure out.

Thanks for your time,

iagman

Share this post


Link to post
Share on other sites

If you are refering to the stringsplit I used then be aware that I just used that to demonstrate the return values. The usable part of the code is inside the For / Next loop which is also not requred. I worked out another one after that post and when I get back on that system I can post it if you want.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Here is a Function that should do what you want.

#include<String.au3>
$iVal = "15,987.6"
_NumToCurr($iVal) ;; Will return the formatted string with thousands separator.
_NumToCurr($iVal, 0);; Will return the formatted string without the thousands separator

Func _NumToCurr($iNum, $iSep = 1)
   $iNum = StringRegExpReplace(StringLeft($iNum, StringLen($inum)-3), "\D", "") & StringRight($iNum, 3)
   Local $Rtn = DllCall("kernel32.dll","int","GetCurrencyFormat","int",0,"int",0,"str",$iNum, _
   "int",0,"str",0,"int",20)
   If $iSep <> 1 Then $Rtn[5] = _StringAddThousandsSep($Rtn[5],"")
   Return $Rtn[5]
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Okay, success at last!

The DLL call did the trick after I realized it was returning an array which I had not intercepted properly.

As a added benefit to this method, dollar signs and commas get added to the mix. I wasn't expecting this and had made up the dialog box with dollar signs already in place.

With revise the GUI and proceed on to the next OOPS.

Thanks for everyone's help. I've saved all your information for reference in future. I'm still determined to understand why the StringFormat is such a clumsy and difficult operation for me to achieve.

iagman

Share this post


Link to post
Share on other sites

Okay, success at last!

The DLL call did the trick after I realized it was returning an array which I had not intercepted properly.

As a added benefit to this method, dollar signs and commas get added to the mix. I wasn't expecting this and had made up the dialog box with dollar signs already in place.

With revise the GUI and proceed on to the next OOPS.

Thanks for everyone's help. I've saved all your information for reference in future. I'm still determined to understand why the StringFormat is such a clumsy and difficult operation for me to achieve.

iagman

StringFormat() failed for the same reason. That's why I used the StringRegExpReplace(). You could have done the same thing with

$sVal = StringReplace($sVal, "$", "");; Remove the dollar sign if it exists
$sVal = _StringAddThousandsSep($sVal,"");; Strip any thousands separators that may exist

I think the DLL call may return different results based on locale. Not all placed use the dot for the decimal separator. To allow for different local styles I opted for the regexp


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0