[REBOL] Round function Re:(3)

1999-01-17 Thread bo


How about this function for rounding positive or negative numbers:

round: func [R [integer! decimal!]][
to-integer R + (-1 * to-integer negative? R) + .5
]

Shows one of the lesser-known uses of logic values in REBOL :-)


On 18-Nov-1999/22:19:59-6:00, [EMAIL PROTECTED] wrote:
>But Russell,, if you do that with a negative number, you round in the wrong
>direction, yes?
>
>Kat
>
>- Original Message -
>From: <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Sent: Thursday, November 18, 1999 9:43 PM
>Subject: [REBOL] Round function Re:
>
>
>> I've always rounded by unconditionally  adding 0.5 then converting to
>> integer
>> Russell [EMAIL PROTECTED]
>> - Original Message -
>> From: <[EMAIL PROTECTED]>
>> To: <[EMAIL PROTECTED]>
>> Sent: Thursday, November 18, 1999 8:00 PM
>> Subject: [REBOL] Round function
>>
>>
>> > ; It's fairly basic, but does the job.
>> >
>> > Round: func [R [integer! decimal!]] [
>> > to-integer either 0.5 <= remainder R 1 [R + 1][R]
>> > ]
>> >
>> > Any one got any other maths functions?
>> >
>> > Andrew Martin
>> > [EMAIL PROTECTED]
>> > http://members.xoom.com/AndrewMartin/
>> > Online @ 33,600 Baud!
>> > -><-
>> >
>> >
>>
>>
>
-- 
   Bohdan "Bo" Lechnowsky
   REBOL  Adventure Guide
   REBOL Technologies 707-467-8000 (http://www.rebol.com)
  Download the REBOL Messaging Language for all Platforms



[REBOL] Round function Re:(3)

1999-11-22 Thread 70740 . 503

Donald,
 
Try the script below. It rounds to n digits after the decimal point. n may
even be negative. Use do read %filename to load the script.

Jerry 


round: func [x[integer! decimal!] n[integer!]]
[
   either x >= 0
   [
  x: (x * (10 ** n)) + 0.5
  x: x - (x // 1)
  x: x * (10 ** - n)
   ]
   [x: - round - x n]
]