Skip to content

Rounding

Swifter edited this page Oct 23, 2024 · 6 revisions
  • rm.roundTo lets you round a number to the nearest multiple of another number.

rm.roundTo(input, step)

rm.roundTo(1, 1) // 1
rm.roundTo(1, 2) // 2
rm.roundTo(1, 3) // 0
  • rm.floorTo lets you floor a number to the nearest multiple of another number.

rm.floorTo(input, step)

rm.floorTo(5, 1) // 5
rm.floorTo(5, 2) // 4
rm.floorTo(5, 3) // 3
  • rm.ceilTo lets you ceil a number to the nearest multiple of another number.

rm.ceilTo(input, step)

rm.ceilTo(3, 1) // 1
rm.ceilTo(3, 2) // 4
rm.ceilTo(3, 3) // 3
  • rm.clamp lets you ensure a number stays between a minimum and maximum.

rm.clamp(input, min, max)

rm.clamp(0, 1, 3) // 1
rm.clamp(2, 1, 3) // 2
rm.clamp(4, 1, 3) // 3

You can also leave either the low bow or the high bound as undefined to ignore them.

// Ignore minimum
rm.clamp(0, undefined, 3) // 0
rm.clamp(4, undefined, 3) // 3
// Ignore maximum
rm.clamp(0, 1, undefined) // 1
rm.clamp(4, 1, undefined) // 4
  • rm.positiveMod will perform a modulo operation such that the output is always positive.
-0.3 % 1 // -0.3

rm.positiveMod(-0.3, 1) // 0.7

Blue is x % 1, red is rm.positiveMod(x, 1)

alt text

  • rm.setDecimals lets you set the decimal place precision of a number.
rm.setDecimals(Math.PI, 2) // 3.14
rm.setDecimals(Math.PI, 3) // 3.142
rm.setDecimals(Math.PI, 4) // 3.1416
rm.setDecimals(Math.PI, 5) // 3.14159
rm.setDecimals(Math.PI, 6) // 3.141593

Note: If you're thinking about having to do this to save file size on your map, that is already handled by settings.

Clone this wiki locally