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

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

From: Jay Reeve <jktr@...>
Date: Thu, 5 Jul 01 23:52:03 -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,

Sorry about that abortive post that slipped out unbidden. As it began to 
suggest, the fn you posted doesn't seem to work correctly in my tests. If 
you want to proceed in swatting the bugs so it will work, then stop 
reading here. If what you want is a working fn, the one below appears to 
me to work correctly. It is clearly NOT what I would call an elegant 
solution, but I haven't come up with a better one.

 0"0
 =J= a  y
  "

LOCAL FN roundNumber#( dblNumber AS DOUBLE, btPlaces )
dim &&
dim result as str255
dim tmp    as double
dim decimal
result = str$( dblNumber )
decimal = instr( 1, result, "." )
long if decimal
long if val( mid$( result, decimal + btPlaces + 1, 1 ) ) > 4
result = str$( dblNumber + 10 ^ -btPlaces )
end if
result = left$( result, decimal + btplaces )
end if
END FN = val( result )

// Example-- run in Console mode

DIM inputNumber   AS DOUBLE
DIM decimalPlaces AS INTEGER

inputNumber       = 0.123456789'9.87654321
print "Input number =";inputNumber
print 
for decimalPlaces = 0 to 10

PRINT decimalPlaces, FN roundNumber#( inputNumber, decimalPlaces )

next