Easy format #'s (Windows only)

Share your advanced PureBasic knowledge/code with the community.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Easy format #'s (Windows only)

Post by jassing »

I had rolled my own number formatter (only to add commas as separaters) Then I bumped into some old fox code; and thought this was "easier" (and as it turns out; faster)

Code: Select all

#numsize = 20
Procedure.s FormatNumber( nNumber )
	Protected *p, sNumber.s, nLen
  *p = AllocateMemory(#numsize)
  nLen = GetCurrencyFormat_(0,0,Str(nNumber),0,*p,#numsize)
  sNumber = PeekS(*p+1,nLen-5) ; get rid of $ and trailing .00
  FreeMemory(*p)
  ProcedureReturn sNumber
EndProcedure
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Easy format #'s (Windows only)

Post by STARGÅTE »

dont work on my system:

Code: Select all

Debug FormatNumber(123456)
23.456,0
because in my system GetCurrencyFormat_ returns:
123.456,00 €
its easier to use some like:

Code: Select all

Procedure.s FormatNumber(Number)
	Protected String.s = Str(Number)
	Protected Position = Len(String)-2
	While Position > 1
		String = InsertString(String, ",", Position)
		Position - 3
	Wend
	ProcedureReturn String
EndProcedure

Debug FormatNumber(123)
Debug FormatNumber(12345)
Debug FormatNumber(1234567)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
HeX0R
Addict
Addict
Posts: 1205
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Easy format #'s (Windows only)

Post by HeX0R »

Aaah, since when does this InsertString() function exists? :shock:

So many senseless seconds gone by, writing Left() + "insert" + Mid()...
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Easy format #'s (Windows only)

Post by STARGÅTE »

PureBasic 4.40 Beta1 released!
- Added: ReverseString(String$), InsertString(String$, StringToInsert$, Position), RemoveString(String$, RemoveString$ [, Mode [, StartPosition [, NbOccurences]]])
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Easy format #'s (Windows only)

Post by jassing »

STARGÅTE wrote:dont work on my system:
Grr. I had thought that the 1st parameter is a locale code and 0 = US - but it looks like it is "system_default".
Post Reply