Page 1 of 1

Formatting a number (commas)

Posted: Sun Nov 20, 2011 12:02 pm
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.

Re: Formatting a number (commas)

Posted: Sun Nov 20, 2011 1:54 pm
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

Re: Formatting a number (commas)

Posted: Mon Nov 21, 2011 7:58 am
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)

Re: Formatting a number (commas)

Posted: Mon Nov 21, 2011 4:28 pm
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

Re: Formatting a number (commas)

Posted: Sun Jan 27, 2013 3:22 pm
by davido
Hi Michael,

Looking for a number formatter to save trying myself.

Chanced on this one; its great and so simple.

Thanks

Dave

Re: Formatting a number (commas)

Posted: Sun Jan 27, 2013 4:18 pm
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.

Re: Formatting a number (commas)

Posted: Sun Jan 27, 2013 4:44 pm
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?).

Re: Formatting a number (commas)

Posted: Sun Jan 27, 2013 4:59 pm
by mk-soft

Re: Formatting a number (commas)

Posted: Sun Jan 27, 2013 5:32 pm
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