roundDecimal function Null safety

double roundDecimal(
  1. double number,
  2. int decimalPlaces
)

Rounds a num to double whereby the decimalPlaces represents the power of 10 you want to round to such that:

roundDecimal(31.141, -2) // => 0.0 (since its rounding between 100 & 0)
roundDecimal(31.141, -1) // => 30.0
roundDecimal(31.141, 0) // => 31.0
roundDecimal(31.141, 1) // => 31.1
roundDecimal(31.141, 2) // => 31.14
// etc...

Implementation

double roundDecimal(double number, int decimalPlaces) {
  double factor = pow(10, decimalPlaces).toDouble();
  return (number * factor).round() / factor;
}