Skip to content

NumberUtils

Dan M edited this page Mar 28, 2022 · 2 revisions

The NumberUtils class contains function for generic number manipulation, from rounding to n places or wrapping between two values.


API

Static Methods

static double roundToPlaces(double val, int places)

Takes in val and will return it rounded to the nearest places decimal points. For example:

double num = 54.423;

num = NumberUtils.roundToPlaces(num, 1);

num is now equal to 54.4.

static double wrapDouble(double val, double min, double max)

Wraps val between min and max. For example:

for (double i = 0; i < 10; i++) {
  print(NumberUtils.wrapDouble(i, 2.0, 5.0));
}

The output of this will be:

4.0
5.0
2.0
3.0
4.0
5.0
2.0
3.0
4.0
5.0

static int wrapInt(int val, int min, int max)

Exactly the same as static double wrapDouble(double val, double min, double max), but works with integers instead.

Clone this wiki locally