Author |
Message |
MihaiG
|
Posted: Thu Jan 06, 2005 6:36 pm Post subject: Rounding |
|
|
is ther any way to make a prcedure that rounds a number to the nearcest digit?? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Thu Jan 06, 2005 6:47 pm Post subject: (No subject) |
|
|
To the nearest digit? Umm... round(r : real) : int |
|
|
|
|
|
xHoly-Divinity
|
Posted: Thu Jan 06, 2005 7:01 pm Post subject: (No subject) |
|
|
use :1:n
e.g.
put 53.423:1:2 <-- that will round to 2 decimal places
put 53.424:1:3 <-- that will round to 3 decimal places
etc. etc. etc. |
|
|
|
|
|
Cervantes
|
Posted: Thu Jan 06, 2005 7:08 pm Post subject: (No subject) |
|
|
That will work if you want to just put the rounded number on the screen in standard text. But, what if you want to round the number and store it as a variable? |
|
|
|
|
|
Martin
|
Posted: Thu Jan 06, 2005 7:30 pm Post subject: (No subject) |
|
|
code: | fcn Round (n : real, accuracy: int) : real
%accuracy is the number of decimal places you want it to be rounded to
%for example, to get 4.5 out of 4.43, you'd have accuracy = 1.
%to get 4 out of 4.33, you'd have accuracy = 0
return round ((10**accuracy) * n)) / (10**accuracy)
end round |
|
|
|
|
|
|
Genesis
|
Posted: Tue Feb 15, 2005 1:48 am Post subject: (No subject) |
|
|
That doesn't work for me. I changed it to this:
code: | fcn Round (n : real, accuracy: int) : real
%accuracy is the number of decimal places you want it to be rounded to
%for example, to get 4.5 out of 4.43, you'd have accuracy = 1.
%to get 4 out of 4.33, you'd have accuracy = 0
result round ((10**accuracy) * n) / (10**accuracy)
end Round |
(Changed "Return" to "Result" and removed a bracket.) |
|
|
|
|
|
Delos
|
Posted: Tue Feb 15, 2005 10:43 am Post subject: (No subject) |
|
|
Or you could use the floor() or ceil() commands. |
|
|
|
|
|
Drakain Zeil
|
Posted: Tue Feb 15, 2005 5:00 pm Post subject: (No subject) |
|
|
For rounding up or down anyway. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|