Page 1 of 1

print float two digit after point

Posted: Tue Mar 27, 2007 11:03 am
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.

Posted: Tue Mar 27, 2007 11:12 am
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.

Posted: Tue Mar 27, 2007 11:16 am
by Flype

Code: Select all

Debug StrF(6.1625, 2) ; for floats  .f (32bits)
Debug StrD(6.1625, 2) ; for doubles .d (64bits)
:?:

Re: print float two digit after point

Posted: Tue Mar 27, 2007 12:54 pm
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.

thank you!

Posted: Wed Mar 28, 2007 6:41 am
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 :-)

Re: thank you!

Posted: Wed Mar 28, 2007 8:24 am
by traumatic
Fable Fox, what is it you don't like about StrF() ?

Code: Select all

Debug StrF(1234.5678, 2) ; outputs 1234.57