In Java, there's System.out.printf() and String.format() that let's you format number/string with commas/whitespace
How do I do this in PB?
In a case like print an integer in console like "1,000,000" rather than 1000000.
Formatting a number (commas)
-
- Addict
- Posts: 4777
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Formatting a number (commas)
A pretty simple and straightforward way to do it is this (not optimized for speed, though):Inertially wrote:In a case like print an integer in console like "1,000,000" rather than 1000000.
Code: Select all
Procedure.s InsertSeparators (number$, separator$=" ", groupSize=3)
Protected i.i, ret$=""
i = Len(number$) - groupSize + 1
While i >= 2
ret$ = separator$ + Mid(number$, i, groupSize) + ret$
i - groupSize
Wend
ProcedureReturn Left(number$, i + groupSize - 1) + ret$
EndProcedure
;-- Demo
If OpenConsole() = 0
Debug "Error"
End
EndIf
PrintN(InsertSeparators("10", ","))
PrintN(InsertSeparators("100", ","))
PrintN(InsertSeparators("1000", ","))
PrintN(InsertSeparators("10000", ","))
PrintN(InsertSeparators("100000", ","))
PrintN(InsertSeparators("1000000", ","))
PrintN(InsertSeparators("10000000", ","))
PrintN(InsertSeparators("100000000", ","))
PrintN(InsertSeparators("1000000000", ","))
PrintN("")
Print("Press [Enter] to continue ...")
Input()
CloseConsole()
Re: Formatting a number (commas)
Windows API solution...
Code: Select all
; Number.s - Number To format (in string format)
; Group.l - group in bunches of
; DecDig.l - number of decimal places
; DecSep.s - Decimal separator character
; GrpSep.s - Group separator Character
; Neg.l - Format negative values
; 0 = (000)
; 1 = -000
; 2 = - 000
; 3 = 000-
; 4 = 000 -
ProcedureDLL.s FormatNum(Number.s,Group.l,DecDig.l,DecSep.s,GrpSep.s,Neg.l);Format Number String
Protected Buffer.s
Buffer.s=Space(255)
NF.NUMBERFMT\NumDigits=DecDig
NF\LeadingZero=0
NF\Grouping=Group
NF\lpDecimalSep=@DecSep
NF\lpThousandSep=@GrpSep
NF\NegativeOrder=Neg
GetNumberFormat_(0,0,Number,NF,@Buffer,Len(Buffer))
ProcedureReturn Buffer
EndProcedure
Debug FormatNum("1000000",3,0,".",",",1)
Debug FormatNum("-1000000",3,2,".",",",0)
- Michael Vogel
- Addict
- Posts: 2797
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Formatting a number (commas)
One additional way...
Code: Select all
Procedure.s DotStr(i.l)
Protected s.s=Str(i)
i.l=Len(s)
While i>3
i-3
s=InsertString(s,".",i+1)
Wend
ProcedureReturn s
EndProcedure
Re: Formatting a number (commas)
Hi Michael,
Looking for a number formatter to save trying myself.
Chanced on this one; its great and so simple.
Thanks
Dave
Looking for a number formatter to save trying myself.
Chanced on this one; its great and so simple.
Thanks
Dave
DE AA EB
Re: Formatting a number (commas)
If you are going to display the resulting string to a user,
I'd recommend using the API function Paul called in his example.
You can try the following procedure which will format a number according to the
current user's system settings (which reflect his country settings).
Also it's possible to extend the function by requesting locale information manually
to have more control over how the number is being formatted.
Unfortunately PB doesn't have any functions (to my knowledge) that allow localizing applications.
I'd recommend using the API function Paul called in his example.
You can try the following procedure which will format a number according to the
current user's system settings (which reflect his country settings).
Also it's possible to extend the function by requesting locale information manually
to have more control over how the number is being formatted.
Code: Select all
Procedure.s FormatNumber(number.s)
Protected length.i
Protected formatted.s
If number = ""
ProcedureReturn ""
EndIf
length = GetNumberFormat_(#LOCALE_USER_DEFAULT, 0, @number, 0, 0, 0)
If Not length
ProcedureReturn ""
EndIf
formatted = Space(length)
If Not GetNumberFormat_(#LOCALE_USER_DEFAULT, 0, @number, 0, @formatted, length)
ProcedureReturn ""
EndIf
ProcedureReturn formatted
EndProcedure
Debug FormatNumber("1000000000000000000.97")
Debug FormatNumber("38439284234")
Debug FormatNumber("324829342.1234")
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Re: Formatting a number (commas)
there is a sprintf() sort of purebasic function which replicates the one that is available on C somewhere on these forums, can't seem to find it (might be on the german forums?).

Re: Formatting a number (commas)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Formatting a number (commas)
Hi mk-soft,
My search was simply for a comma formatted procedure.
Wow! Your procedure does just about everything. Although I am not acquainted with sprintf() I'll probably use it.
Thanks very much to everyone who responded. It's great.
Dave
My search was simply for a comma formatted procedure.
Wow! Your procedure does just about everything. Although I am not acquainted with sprintf() I'll probably use it.
Thanks very much to everyone who responded. It's great.


Dave
DE AA EB