print float two digit after point

Just starting out? Need help? Post your questions and find answers here.
Fable Fox
User
User
Posts: 29
Joined: Fri Dec 12, 2003 4:52 am

print float two digit after point

Post by Fable Fox »

go to http://www.fablefox.com

anyway, I haven't program in PB for more than a year already. So I wrote a simple app just out of boring. how do I print float just two digit after point. i know i can do it by change it to string, delete all charater after 2 from the point, and turn it back to float again.

any faster and easier way, hmmm?

my brain too rusty and right now i'm writing novel and try to learn piano anyway.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The problem with using floats like this is when converting back to a float. The inherent errors in rounding mean that you will likely not end up with just two decimal places etc. even when there is nothing wrong with your code.

E.g. the following will do as you requested:

Code: Select all

a.d = 6.1625
b.d=Int(100*a)/100

Debug b
but does not work properly because of the limitations with the representation of floating point numbers.

Your best bet is to follow your original plan, but keep the result in string form rather than convert back to a float.
I may look like a mule, but I'm not a complete ass.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Code: Select all

Debug StrF(6.1625, 2) ; for floats  .f (32bits)
Debug StrD(6.1625, 2) ; for doubles .d (64bits)
:?:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: print float two digit after point

Post by Kaeru Gaman »

Fable Fox wrote:... how do I print float just two digit after point...
for printing, use a string. so just use the conversion like Flype posted.
but do not convert it back into a float, do further calculations with the original float.

an other alternative is: use fixcomma-values.
if you are doing calculations with currency, it's better anyways.
oh... and have a nice day.
Fable Fox
User
User
Posts: 29
Joined: Fri Dec 12, 2003 4:52 am

thank you!

Post by Fable Fox »

Thanks everyone, I slept on the problem and realized that I only need it for the printing. So I use SearchString for "." and use Mid just to take that "." position plus two digit.

but there is a simple error, 1234.5678 will be 1234.56, and not 1234.57. I can do the calculation, but it's not a big thing anyway.

I'll check all suggestion for future releases.

Thanks :-)
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: thank you!

Post by traumatic »

Fable Fox, what is it you don't like about StrF() ?

Code: Select all

Debug StrF(1234.5678, 2) ; outputs 1234.57
Good programmers don't comment their code. It was hard to write, should be hard to read.
Post Reply