[futurebasic] Re: [FB] Function to round any number

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2001 : Group Archive : Group : All Groups

From: Chris Stasny <staz@...>
Date: Fri, 6 Jul 2001 05:51:17 -0500
>  >Description:
>  >This function returns a fractional number representation of expr#
>  >(it rounds off user specified digits to the right of the decimal
>  >point). The number it returns is a double-precision floating-point
>  >value.
>
>Ken,
>
>I ran your code through a loop to see the number you supplied rounded to
>decimal places 0-15:
>
>number = 259.44578685
>  0               260     << This doesn't look right
>  1               259.5     << This doesn't look right
>  2               259.45
>  3               259.446
>  4               259.4458
>  5               259.44579
>  6               259.445787
>  7               259.4457869
>  8               259.44578686     << This doesn't look right
>  9               259.44578685
>  10              259.44578685
>  11              259.44578685
>  12              259.44578685
>  13              259.44578685
>  14              259.44578685
>  15              259.44578685

I don't know if this approach is better or worse, but FB 
automatically rounds using the USING statement. I built something 
based on that approach.

local
dim t$
local fn roundDecimal$(theFloat#,places)
t$=string$(16,"#")+"."+string$(places,"#")
t$=using t$;theFloat#
while t$[1]=_" " and t$[0]
t$=mid$(t$,2)
wend
end fn = t$


dim x
for x = 0 to 16
print fn roundDecimal$(259.44578685,x)
next

stop


The result shows the faults of PPC math, but is still fairly accurate.

259.
259.4
259.45
259.446
259.4458
259.44579
259.445787
259.4457868
259.44578685
259.445786850
259.4457868500
259.44578685000
259.445786850000
259.4457868500000
259.44578684999999
259.445786849999990
259.4457868499999904



Best,

-STAZ  ~)~

800.348.2623 Orders  http://www.stazsoftware.com
228.255.7086 FAX     mailto:staz@...