round<T extends num> static method

T round<T extends num>(
  1. num n, [
  2. int precision = 0
])

Rounds n to a precision of precision. The returned value is of the same type as n.

Implementation

static T round<T extends num>(num n, [ int precision = 0 ]) {
	if (precision == 0) {
		if (T == double || T == num) {
			return n.roundToDouble() as T;
		}
		else if (T == int) {
			return n.round() as T;
		}
		else {
			throw "can't round to type [${T}]";
		}
	}

	num factor = Math.pow(10, precision);
	return EZ.round<T>(n * factor) / factor as T;
}