PDA

View Full Version : Rounding a number, How?

 mrapelje02-23-2004, 04:45 AMI 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.
Bobo
02-23-2004, 05:26 AM
Originally posted by mrapelje
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.

"ceil" goes up, "floor" goes down.
Found under "Number Values" topic.

mrapelje
02-23-2004, 05:42 AM
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.

Bobo
02-23-2004, 05:51 AM
Originally posted by mrapelje
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.

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.

magicm
02-23-2004, 08:29 AM
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 =
(
&nbsp;&nbsp;&nbsp;&nbsp;local mult = 10.0 ^ n
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(floor ((val * mult) + 0.5)) / mult
)

-- example
(round_to 3.625 0) as integer

- Martijn

CGTalk Moderation
01-17-2006, 12:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.