Truncation function

14 views
Skip to first unread message

Shadow Raider

unread,
Jan 8, 2009, 6:50:49 AM1/8/09
to REBOL
Hello everybody,
do you know if it exists a function for correct number truncation?
Examples:

a: 5.44321
a: trunc 2 a
print a
== 5.44

b: 5.44621
b: trunc 2 b
print b
== 5.45

b: 5.44621
b: trunc 3 b
print b
== 55.446

sqlab

unread,
Jan 8, 2009, 8:09:03 AM1/8/09
to Re...@googlegroups.com
round can deliver that


>> help round
USAGE:
ROUND n /even /down /half-down /floor /ceiling /half-ceiling /to scale

DESCRIPTION:
Returns the nearest integer. Halves round up (away from zero) by
default.
ROUND is a function value.

ARGUMENTS:
n -- The value to round (Type: number money time)

REFINEMENTS:
/even -- Halves round toward even results
/down -- Round toward zero, ignoring discarded digits. (truncate)
/half-down -- Halves round toward zero
/floor -- Round in negative direction
/ceiling -- Round in positive direction
/half-ceiling -- Halves round in positive direction
/to -- Return the nearest multiple of the scale parameter
scale -- Must be a non-zero value (Type: number money time)

>> round/to/even a 0.01
== 5.44

round/to b 0.01
== 5.45

>> round/to/even b 0.001
== 5.446

Shadow Raider

unread,
Jan 8, 2009, 12:43:57 PM1/8/09
to REBOL
It works! Great!!!
Thank you very much!
Max

Shadow Raider

unread,
Jan 12, 2009, 4:23:03 AM1/12/09
to REBOL
On 8 Gen, 14:09, sqlab <sq...@gmx.net> wrote:
It seems to work with only "round/to".
Max

Gregg Irwin

unread,
Jan 12, 2009, 10:37:13 AM1/12/09
to Shadow Raider
Hi Max,

SR> It seems to work with only "round/to".

ROUND/DOWN is the equivalent of truncate in other languages. That will
round to the nearest integer.

--Gregg

Reply all
Reply to author
Forward
0 new messages