Formatting a number (commas)

Just starting out? Need help? Post your questions and find answers here.
Inertially
New User
New User
Posts: 9
Joined: Mon Nov 14, 2011 12:24 am

Formatting a number (commas)

Post by Inertially »

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.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Formatting a number (commas)

Post by Little John »

Inertially wrote:In a case like print an integer in console like "1,000,000" rather than 1000000.
A pretty simple and straightforward way to do it is this (not optimized for speed, though):

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()
Regards, Little John
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Formatting a number (commas)

Post by Paul »

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)
Image Image
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Formatting a number (commas)

Post by Michael Vogel »

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
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Formatting a number (commas)

Post by davido »

Hi Michael,

Looking for a number formatter to save trying myself.

Chanced on this one; its great and so simple.

Thanks

Dave
DE AA EB
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Formatting a number (commas)

Post by Shield »

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.

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")
Unfortunately PB doesn't have any functions (to my knowledge) that allow localizing applications.
Image
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
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: Formatting a number (commas)

Post by moogle »

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?).
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Formatting a number (commas)

Post by mk-soft »

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
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Formatting a number (commas)

Post by davido »

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. :!: :D

Dave
DE AA EB
Post Reply