Hi Ralph

To really do correct rounding for all possible decimal values requires a bit
of work in REBOL.  Problems with overflow, round nearest even, etc.  For all
the gory details see the script decimal.r archived at rebol.org. But just
for fun here is a way to use to-money. May fail to give correct results with
decimal! input too large in magnitude or with too many digits of precision.

>> round: func [x][to-decimal replace to-string to-money x "$" ""]
>> round 1.0051
== 1.01
>> round 1.0049
== 1
>> round -1.0049
== -1
>> round -1.0051
== -1.01

BTW This works better than your roundoff function for negative values:

>> roundoff -1.0051
== -1

Cheers
-Larry

----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, July 18, 2000 6:57 PM
Subject: [REBOL] roundoff?


> I needed a simple two decimal place round off function for a script this
> evening.
>
> This is what I came up with:
>
> roundoff: func ["Rounds off to 2 decimal places" a][
> a: a * 100
>         b: a - to-integer a
>         a: to-integer a
>         if b > .5 [a: a + 1]
>         a: divide a 100
>         ]
>
> Here's how it works:
>
> >> roundoff 10.567890
> == 10.57
> >> roundoff 10.563333
> == 10.56
>
> Can anyone improve on this, or is there a function already in REBOL I
> overlooked?
>
> Okay, yes, to-money does it:
>
> >> to-money 10.567
> == $10.57
> >> to-money 10.563
> == $10.56
>
> but I want the decimal! type.
>
> --Ralph Roberts
>
>

Reply via email to