Comma seperated number
BlitzMax Forums/BlitzMax Beginners Area/Comma seperated number
| ||
| I'm looking for a function to seperate numbers with commas, to make it human readable. ie: 999,999,999 Does anyone have this to hand? Many thanks. |
| ||
| would the input be an int or string? -never mind, almost done- |
| ||
SuperStrict
Function CommaNumber$(v:Int,div$=",")
Local s$=v
If s$="" Return ""
Local n$
For Local t:Int=Len(s$)-1 To 0 Step -1
n=Mid(s,1+t,1)+n
If t And Not ((Len(s)-t) Mod 3) n=div+n
Next
Return n
End Function
Function CommaString$(s$,div$=",")
If s$="" Return ""
Local n$
For Local t:Int=Len(s$)-1 To 0 Step -1
n=Mid(s,1+t,1)+n
If t And Not ((Len(s)-t) Mod 3) n=div+n
Next
Return n
End Function
Print CommaString("1")
Print CommaString("12")
Print CommaString("123")
Print CommaString("1234")
Print CommaString("12345")
Print CommaString("123456")
Print CommaString("1234567"," ")
Print CommaString("12345678",".")
Print CommaNumber(10001001,".")
End
|
| ||
Here's my solution...Function FormatNumber:String(num:String) Local i:Int=num.Length-1,j:Int=0,out:String Repeat out=Chr(num[i])+out i:-1 j:+1 If j=3 And i>=0 Then out=","+out j=0 EndIf Until i<0 Return out EndFunction Local num:Int=1 For Local i:Int=1 Until 11 Print FormatNumber(num) num:*10 Next No need for seperate functions for numbers or strings, as they will all get converted to a string when passed to the function. |
| ||
| That's great! Thanks for this! |
| ||
| Whoa. Just use a StringTokenizer (if you want one value at a time) / Splitter (if you want all the values in an array). There are (litterally) hundreds in the Code Archives. |
| ||
None of those seem to work for real numbers :Local num:Float=115600.12 Print FormatNumber(num) 1,156,00.,117 Oh well... I guess it's close enough :-) |
| ||
| I put one in the code archives almost a year ago that does what you want. http://www.blitzbasic.com/codearcs/codearcs.php?code=1790 |
| ||
| None of those seem to work for real numbers It was never intended to. I use it to display a player's score. AD seems satisfied, otherwise they would have mentioned they needed it to display floats. |