roundWithPrecision method

  1. @override
Number roundWithPrecision(
  1. int precision, [
  2. RoundingMode mode = RoundingMode.nearestEven
])
override

Returns number rounded according to precision and RoundingMode.

If precision is negative, number is rounded to hundreds (-2), thousands (-3), etc. In that case, it rounds to nearest, ties away from zero.
If rounded number is integer, rounding mode nearestEven is automatically used and cannot be changed.

Rounding modes: (https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules, https://upload.wikimedia.org/wikipedia/commons/8/8a/Comparison_rounding_graphs_SMIL.svg)

  • nearestEven (0) - rounds to the nearest value; if the number falls midway, it is rounded to the nearest value with an even least significant digit
  • nearestOdd (1) - rounds to the nearest value; if the number falls midway, it is rounded to the nearest value with an odd least significant digit
  • nearestFromZero (2) - rounds to the nearest value; if the number falls midway, it is rounded to the value which is the farthest from zero
  • nearestToZero (3) - rounds to the nearest value; if the number falls midway, it is rounded to the value which is the closest to zero
  • nearestDownward (4) - rounds to the nearest value; if the number falls midway, it rounds down
  • nearestUpward (5) - rounds to the nearest value; if the number falls midway, it rounds down
  • towardsZero (6) - directed rounding towards zero
  • fromZero (7) - directed rounding from zero
  • up (8) - directed rounding towards positive infinity
  • down (9) - directed rounding towards negative infinity

Implementation

@override
Number roundWithPrecision(int precision,
    [RoundingMode mode = RoundingMode.nearestEven]) {
  if (precision >= 0) {
    return Integer(value);
  } else {
    var multiplier = pow(10, -precision) as int;
    final adder = 5 * (multiplier ~/ 10) * ((value < 0) ? -1 : 1);
    return Integer(((value + adder) ~/ multiplier) * multiplier);
  }
}