Jump to content

StringFormat


Recommended Posts

The following code

msgbox(0,"",StringFormat("%10.2f",10000))

Produces the number

10000.00

which produces the correct number of decimal places 'currency' however how can this number be delimited with a comma ',' so that it appears as follows:

10,000.00

Help is always appreciated Ant.. :)

Link to comment
Share on other sites

Well I took a quick jab at it, and here is what I came up with. Do let me know if it works for you :)

#include <String.au3>
#include <Array.au3>

Func _thousandsSeparate($num, $separatorChar = ",", $decimalChar = ".")
    Local $parts = StringSplit($num, $decimalChar)
    Local $stringIntPart = ""
    Local $arrayIntPart = StringSplit(_StringReverse($parts[1]), "")
    For $i = 1 To $arrayIntPart[0]
        $stringIntPart &= $arrayIntPart[$i]
        If (Mod($i, 3) == 0) And ($i <> $arrayIntPart[0]) Then
            $stringIntPart &= $separatorChar
        EndIf
    Next
    $parts[1] = _StringReverse($stringIntPart)
    Return _ArrayToString($parts, $decimalChar, 1)
EndFunc   ;==>_thousandsSeparate

MsgBox(64, "Currency Format Example", "100000 => " & _thousandsSeparate(StringFormat("%.2f", 100000)))

EDIT: Added "And ($i <> $arrayIntPart[0])" condition

Edited by smartee
Link to comment
Share on other sites

Well I took a quick jab at it, and here is what I came up with. Do let me know if it works for you :)

#include <String.au3>
#include <Array.au3>

Func _thousandsSeparate($num, $separatorChar = ",", $decimalChar = ".")
    Local $parts = StringSplit($num, $decimalChar)
    Local $stringIntPart = ""
    Local $arrayIntPart = StringSplit(_StringReverse($parts[1]), "")
    For $i = 1 To $arrayIntPart[0]
        $stringIntPart &= $arrayIntPart[$i]
        If (Mod($i, 3) == 0) And ($i <> $arrayIntPart[0]) Then
            $stringIntPart &= $separatorChar
        EndIf
    Next
    $parts[1] = _StringReverse($stringIntPart)
    Return _ArrayToString($parts, $decimalChar, 1)
EndFunc   ;==>_thousandsSeparate

MsgBox(64, "Currency Format Example", "100000 => " & _thousandsSeparate(StringFormat("%.2f", 100000)))

EDIT: Added "And ($i <> $arrayIntPart[0])" condition

Thats really cool and works perfectly perhaps it should be included into AutoIT Functions thanks for your help which was very much appreciated Ant..
Link to comment
Share on other sites

  • Moderators

Thats really cool and works perfectly perhaps it should be included into AutoIT Functions thanks for your help which was very much appreciated Ant..

Removed: _StringAddThousandsSep() has been removed. Too many opinions on how the function should work means nobody is happy with it.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 4 weeks later...

Due to a requirement to right format some numbers so that a column exists between the right hand border and the input GUI I am now adding a number of spaces to the value $mynumber = $NumInput & " ". This has required a slight modification to the comma delimit currency routine where I have inserted at line 1 a statement which removes any white spaces that may exist in the value passed. Every thing appears to be in working order.

;//Comma Delimit Currency [turns numeric value into a string]
Func _FormatCurrency($Num, $separatorChar = ",", $decimalChar = ".")
 $Num = StringStripWS($Num, 8)
 Local $parts = StringSplit($Num, $decimalChar)
 Local $stringIntPart = "", $Result
 Local $arrayIntPart = StringSplit(_StringReverse($parts[1]), "")
 For $i = 1 To $arrayIntPart[0]
  $stringIntPart &= $arrayIntPart[$i]
  If (Mod($i, 3) == 0) And ($i <> $arrayIntPart[0]) Then
   $stringIntPart &= $separatorChar
  EndIf
 Next
 $parts[1] = _StringReverse($stringIntPart)
 Return _ArrayToString($parts, $decimalChar, 1) 
EndFunc   ;==>_FormatCurrency

Cheers Ant..:unsure::>

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...