Using expressions and get rounded whole number?

lightwriter
What I mean is let's say I have an integer attribute callled example.
example = 5
and then I have a script that changes it when a condition is met.
When that condition is met:
the script runs example = example *1.08+1
but if I display example afterwards it equals a decimal number, however even though it was originally defined as an integer. Is there a way to round this to the nearest whole number?

TinFoilMkIV
yes, there are actually 3 different functions for rounding
round(example)
//will follow standard rounding rules
floor(example)
//will always round down to a whole number, ie: floor(1.99) = 1
ceiling(example)
//the opposite of floor, always rounds up


However if you plan to use this variable and modify it by decimals relatively often, I'd actually keep a second variable. Use one variable to track the 'true value' of your attribute, then the display variable which holds the rounded value for whatever functional purpose you want this attribute to have.

//starting values
example_real = 5
example_display = example_real
//modify the values
example_real = example_real*1.08+1
example_display = round(example_real)
msg("example = " + example_display + ".")


alternatively you could simply use the round function in an expression whenever you want to display the value but leave the actual variable holding a decimal, but this may be less desirable depending on how you plan to use it.

lightwriter
Alright, thanks!

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums