Getting Rid of Decimal (Rounding Up/Down)

GAGE HOLSTON
29 Apr 2020, 03:26

In my game, there is a point where a multiplier of 1.5 is applied to a variable. I want a way to simply either round it up or down, either one is fine.
I've tried looking at past forum posts for an answer, but I don't understand any of the ones I could find.


mrangel
29 Apr 2020, 08:51

There are four functions to round a number:

  • floor - rounds down
  • ceiling - rounds up
  • round - rounds to the closest whole number
  • truncate - discards the part after the decimal

But it might be quicker and easier to stick with integer arithmetic. If a sum includes no decimals, the answer won't either.

  • a = b * 1.5 gives a decimal because there's a decimal "1.5"
  • a = b * 3 / 2.0 gives the same answer
  • but a = b * 3 / 2 gives the same number rounded off (if b is an int), because / only returns the integer part if its arguments are integers.

So if you want your answer to be a whole number, it's easier to work with fractions :)