[Closed] Rounding a number, How?
I need to take a number say 3.625 and round it to it nearest whole number… I.E. 4, Or if someone knows how to truncate a number that would be good too 3.625 –> 3. I have not been able to find this in the reference and or searches. I relize that this is a newbie question but then thats me when it comes to MaxScript.
Thanks.
Originally posted by mrapelje
[B]I need to take a number say 3.625 and round it to it nearest whole number… I.E. 4, Or if someone knows how to truncate a number that would be good too 3.625 –> 3. I have not been able to find this in the reference and or searches. I relize that this is a newbie question but then thats me when it comes to MaxScript.Thanks. [/B]
“ceil” goes up, “floor” goes down.
Found under “Number Values” topic.
Thank you very much Bobo. I saw that in Number Values and:
I tried (3.375)floor –> Error
and not
floor(3.375). –> 3
Should have kept trying.
Thanks again.
Originally posted by mrapelje
[B]Thank you very much Bobo. I saw that in Number Values and:I tried (3.375)floor –> Error
and not
floor(3.375). –> 3
Should have kept trying.
Thanks again. [/B]
The Help says:
floor <number>
Returns the nearest whole number less than or equal to number. The result is always a float.
and not
<number> floor
You don’t need the () around btw.
If you want to round to the nearest integer (floor 3.625 returns 3, not 4), try this function (can also be found in scripts/examples/Function_RoundTo.ms)
– rounds a value to n decimal places
fn round_to val n =
(
local mult = 10.0 ^ n
(floor ((val * mult) + 0.5)) / mult
)
– example
(round_to 3.625 0) as integer
- Martijn