[futurebasic] Rounding

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 1999 : Group Archive : Group : All Groups

From: scram@... (Stu Cram)
Date: Sat, 2 Jan 1999 09:33:06 -0700
Morrison SoftDesign wrote
> Does anyone have a quick way to round up to the nearest 10ths a given dbl val?
> like:    myDouble# = 123.4567
> ..round up somehow...
>          myNewDouble# = 123.46

Alain Pastor suggested this:
>   DEF FN round#(number#) = VAL(USING "###.##";number#)

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.
HTH
Stu Cram