Say i had the following decimals:
0.25, 0.5, 075. 1.00, 1.25
and I wanted them to round up to integers:
1, 1, 1, 1, 2
What kind of snippet would I have to use?
http://www.hans-eric.com/code-samples/ruby-floating-point-round-off/
Perhaps this link will be useful to you?
Rounding comes as part of the Numeric class, which all numbers belong to.
Here's the methods you can use for RMXP for all numbers:
Quote from: RMXP Help File
+ self
Returns self.
- self
Returns self, negated.
This method is defined by the binary operator - in 0 - self.
abs
Returns the absolute value of self. (Disregards the negative sign)
ceil
Returns the smallest integer equal to or greater than self (i.e., the ceiling).
floor
Returns the largest integer that does not exceed self (i.e., the floor).
integer?
Returs true when self is an integer.
round
Returns the integer closest to self.
truncate
Discards the decimal point and all digits after it.
So you can use (assuming that
number is a variable holding an arbitrary number with decimals)
number.round to round to the nearest integer,
number.ceil to force it to round up to the nearest integer and
number.floor to force it to round down to the nearest integer. number.truncate will simply drop all digits after the decimal point.
Edit: added clarity on
number being a variable.
well it took a bit of tinkering with ceil to realize it's like n.ceil and not ceil(n), but it definitely worked. In flash they had something called Math.ceil(n) and Math.floor(n) so I was looking for an equivalent.
Jon Fawkes answer is actually what I was going to go with when I got my first error trying ceil(n). I got confused because of Integer(n) and integer?. :D
Quote from: shintashi on October 22, 2011, 04:23:46 PM
well it took a bit of tinkering with ceil to realize it's like n.ceil and not ceil(n), but it definitely worked. In flash they had something called Math.ceil(n) and Math.floor(n) so I was looking for an equivalent.
I edited my post to make it more clear that number was a variable with .ceil .floor and .round being method calls on those variables.
Jon's link gives you a way of showing you how to patch over existing code to make more user-friendly methods of calling what you want, especially where your more familiar with other languages.