RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
[Resolved] How do you round up decimals?

0 Members and 1 Guest are viewing this topic.

***
Rep:
Level 82
We learn by living...
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?
« Last Edit: October 24, 2011, 04:08:01 PM by shintashi »

**
Rep:
Level 67
Eternal Newbie

*
Rep:
Level 82
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.
« Last Edit: October 23, 2011, 04:14:01 PM by LoganForrests »
(Why do I always feel like it's the end of the world and I'm the last man standing?)

***
Rep:
Level 82
We learn by living...
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

*
Rep:
Level 82
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.
(Why do I always feel like it's the end of the world and I'm the last man standing?)