Page 1 of 1

Easy format #'s (Windows only)

Posted: Sat Jun 18, 2011 4:58 pm
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

Re: Easy format #'s (Windows only)

Posted: Sat Jun 18, 2011 6:16 pm
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)

Re: Easy format #'s (Windows only)

Posted: Sat Jun 18, 2011 7:10 pm
by HeX0R
Aaah, since when does this InsertString() function exists? :shock:

So many senseless seconds gone by, writing Left() + "insert" + Mid()...

Re: Easy format #'s (Windows only)

Posted: Sat Jun 18, 2011 7:17 pm
by STARGÅTE
PureBasic 4.40 Beta1 released!
- Added: ReverseString(String$), InsertString(String$, StringToInsert$, Position), RemoveString(String$, RemoveString$ [, Mode [, StartPosition [, NbOccurences]]])

Re: Easy format #'s (Windows only)

Posted: Sun Jun 19, 2011 7:36 am
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".