Rebol Talk Forum  |  REBOL Discussions  |  REBOL Core  |  Topic: rounding numbers in rebol
Pages: [1] Print
Author Topic: rounding numbers in rebol  (Read 565 times)
jchunn
Newbie
*
Offline Offline

Posts: 2


View Profile
rounding numbers in rebol
« on: June 22, 2006, 11:56:43 AM »

Hello,
Can someone show me how to round a number to 2 decimal places?
Spent all morning trying the round function, but can't seem to
get it.
Thanks,
Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
Re: rounding numbers in rebol
« Reply #1 on: June 22, 2006, 03:56:30 PM »

Use ROUND/TO and add a decimal value.  ie...

>> round/to 123.4567 .05
== 123.45
>> round/to 123.4567 .005
== 123.455

(Someone else can explain how the different decimal values affect the result;)

Or if it's just formatting you want, then your number will have to be turned into a string.  With this being one way to do it...

>> n: 123.4567
>> head clear next next find/tail join mold to-decimal n "00" #"."
== "123.45"
>> n: 1
== 1
>> head clear next next find/tail join mold to-decimal n "00" #"."
== "1.00"

Which could be improved by turning it into a function...
Code:
format: func [
    num [integer! decimal!]
    places [integer!]
][
    head clear skip find/tail join mold to-decimal num "00" #"." places
]
Which allows...

>> format 123.4567 2
== "123.45"
>> format 123.4567 3
== "123.456"
>> format 123.4 3
== "123.400"
>> format 1 2
== "1.00"

Hope that helps.
Logged

- Carl Read
jchunn
Newbie
*
Offline Offline

Posts: 2


View Profile
Re: rounding numbers in rebol
« Reply #2 on: July 01, 2006, 04:20:47 PM »

Thank you very much, Carl!
Sincerely,
Jan Chunn
Logged
Pages: [1] Print 
Rebol Talk Forum  |  REBOL Discussions  |  REBOL Core  |  Topic: rounding numbers in rebol
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 21, 2008, 05:36:52 PM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2295 Posts in 593 Topics by 3758 Members
Latest Member: Xhzatkpf

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc