Archive: Check out my function


Check out my function
Hello all.

I am working with a bunch of numbers in an NSIS script I made, and wanted to add the commas (1,450,884) to them to make them easier to read. I put this little function together because I could not find anything on the Wiki. Feel free to point out anything I may have missed that would have done what this does, or if there is a better way to program what I did below. Thanks all.

- Handles up to 12 digit numbers
- Sets error flag if over 12 digits
- Ignores numbers 3 digits and less
- Preserves stack and register variables values once complete(I think :rolleyes: )



Push $0 ;$0 = 1450884
Call CommaNum
Pop $0 ;$0 = 1,450,884

Function CommaNum
Exch $0
Push $1
Push $2
Push $3
Push $4
Push $5

StrLen $1 $0
IntCmp $1 3 Done Done
IntCmp $1 6 0 0 +5
StrCpy $2 $0 -3
StrCpy $3 $0 "" -3
StrCpy $0 "$2,$3"
Goto Done
IntCmp $1 9 0 0 +6
StrCpy $2 $0 -6
StrCpy $3 $0 3 -6
StrCpy $4 $0 "" -3
StrCpy $0 "$2,$3,$4"
Goto Done
IntCmp $1 12 0 0 Error
StrCpy $2 $0 -9
StrCpy $3 $0 3 -9
StrCpy $4 $0 3 -6
StrCpy $5 $0 "" -3
StrCpy $0 "$2,$3,$4,$5"
Goto Done
Error:
SetErrors

Done:
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Exch $0
FunctionEnd

Well by the looks of it you could have just used a single loop to put in a comma character after each 3 digits? Unless there's something that I am missing.

-Stu


You can also call GetNumberFormat using the System plug-in.


Good to know, thanks guys for the information.

Jnuw


Well by the looks of it you could have just used a single loop to put in a comma character after each 3 digits?
Yea, something like:
Name "Output"
OutFile "Output.exe"

Function CommaNum
Exch $1
Exch
Exch $0
Exch
Push $2
Push $3
Push $4

StrLen $2 $0
IntOp $3 $2 % 3
StrCmp $3 0 0 +2
StrCpy $3 3
StrCpy $4 $0 $3

StrCpy $2 $0 3 $3
StrCmp $2 '' +4
StrCpy $4 '$4$1$2'
IntOp $3 $3 + 3
goto -4
StrCpy $0 $4

Pop $4
Pop $3
Pop $2
Pop $1
Exch $0
FunctionEnd

Section
Push "1234567890"
Push ","
Call CommaNum
Pop $0 # 1,234,567,890

MessageBox MB_OK '$$0={$0}'
SectionEnd

Thanks Instructor, nice enhancements to it.