round<T extends num> function

double round<T extends num>(
  1. T value,
  2. [int digits = 0]
)

Rounds to given digit count.

If digits is positive, the number is rounded to the nth digit after the decimal point. If digits is negative, the number is rounded to the nth digit in front of the decimal point. Rounds away from zero on n.5: round(35, -1) == 40 and round(-35, -1) == -40 round(1.25, 1) == 1.3 and round(-1.25, 1) == -1.3

Implementation

double round<T extends num>(T value, [int digits = 0]) {
  if (value is double && (value.isNaN || value.isInfinite)) {
    return value;
  }

  final p = math.pow(10, 0 - digits);
  return ((value / p).round() * p).toDouble();
}