|
Pages: [1]
|
 |
|
Author
|
Topic: rounding numbers in rebol (Read 565 times)
|
|
jchunn
|
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
|
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... 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
|
Thank you very much, Carl! Sincerely, Jan Chunn
|
|
|
|
|
Logged
|
|
|
|
|
|
Pages: [1]
|
|
|
 |