Format Numbers

Share your advanced PureBasic knowledge/code with the community.
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: Format Numbers

Post by Frarth »

WilliamL wrote:That's a good question. I'm using StrF(#,number of digits after decimal point) and it seems to round up (the rules are made by StrF() ). There needs to be more thought in how the rounding is done. I was most interested in getting, for example the floating 12.34 to be converted into a string as '12.34' and not '12.3399999', and not the problem of 3.66666 being rounded up or 3.3333 not being rounded up.

Actually, this was just an exercise and this was a way to clarify my thinking of how the process could be done. I think it would be nice if a single format function could be developed for everyone (multi-platform) to use in place of having a format function built-in in pb. I am certainly not offering this as that example but as encouragement for further development which might not be this code at all.
StrF() is less accurate than StrD(). Look at what my code above does with:

Code: Select all

Debug FormatF(12.34, "##.########")
Debug FormatD(12.34, "##.########")
'12.3399999', is normal because Single floating point is only accurate up to 7 digits (including decimal point) Anyone correct me if I'm wrong.
PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Format Numbers

Post by WilliamL »

[deleted]
Last edited by WilliamL on Sun Jan 30, 2011 10:07 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: Format Numbers

Post by Frarth »

I'd say, keep things simple. Format procedures for different types usually work faster. Also, I'm not exactly sure what you try to do with US currency. Placing the right symbol in front, isn't that enough?
PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Format Numbers

Post by Kaeru Gaman »

putting a $ in front of a number will cause Str() to interprete it as Hex.

however, float is not suitable for currency.
use quad and calculate in 1/10 or 1/100 cent, insert a comma for output only.
oh... and have a nice day.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Format Numbers

Post by WilliamL »

You got that right! I know what I do with (currency), I spend it and there is never enough. :mrgreen:

@Kaeru - I agree with you! I wrote my accounting program using integers then a format procedure for display.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply