MATHEMATICS
Round double precision values
Maybe you could define a function that performs the task, but probably there is a fastest way that I don't know.
DEF FN round#(number#) = VAL(USING "###.##";number#)
WINDOW 1
myDouble# = 123.4567
myNewDouble# = FN round#(myDouble#)
PRINT myNewDouble#
DO
UNTIL FN BUTTON OR LEN(INKEY$)
Alain
I may be mistaken, but I think the preferences rounding is only in relation to ## formatting.
Been doing some traveling and not following everything, but...
I personally do my own rounding because FB comes with a round value of 49 and I prefer 50. I always forget to reset it if I do an install. Perhaps something like this to make a real round to the nearest one :
LONG IF myReal!>0
myReal! = myReal! + 0.5 'round up
END IF
myInteger = INT(myReal!)
George
Another method is to multiply your variable by 100, add 1 / 2, convert that to an integer, and then divide by 100. This avoids number to string conversions and handles numbers bigger than 999.99 which may be the limit in Alain's method without changing the using format string.
 Like this :
DEF FN round#(number#) = INT(number# * 100.0 + 0.5) / 100.0
 BTW, to round to 3 places, use 1000 in place of 100.
Stu Cram
I'd call it an "interesting" question. :)
First of all, you can (if you want) use the USING function without a PRINT statement. This is one way to do what you want:
roundedNum! = VAL(USING "#######.##"; originalNum!)
Another way, that doesn't involve an intermediate string conversion, is this:
roundedNum! = INT(originalNum! * 100 + 0.5) / 100
Rick
The often quoted method of rounding to 2 decimals is
result# = INT( num#*100 + 0.5 ) \ 100
This will work for positive numbers but not negatives as INT 'rounds down' to the next lower integer.
The following function can be used to round to as many decimals places as you may want. Note that using 2 for the # of decimals will round to the nearest hundred.
LOCAL FN Round#( Num#, numDecimalPlaces )
pwr# = 10^numDecimalPlaces
result# = SGN(Num#) * INT(ABS(Num#)*pwr#+.5)\pwr#
END FN = result#
Stu Cram
