Answers for "flutter double round up by to 2 decimal places"

2

dart round to 2 decimals

import 'dart:math';

double roundDouble(double value, int places){ 
   double mod = pow(10.0, places); 
   return ((value * mod).round().toDouble() / mod); 
}

main() {
  double num1 = roundDouble(12.3412, 2);
  // 12.34

  double num2 = roundDouble(12.5668, 2);
  // 12.57

  double num3 = roundDouble(-12.3412, 2);
  // -12.34

  double num4 = roundDouble(-12.3456, 2);
  // -12.35
}

OR

double num1 = double.parse((12.3412).toStringAsFixed(2));
// 12.34
Posted by: Guest on September-14-2021
2

double 2 decimal places flutter

/// Define an extension:
extension Ex on double {
  double toPrecision(int n) => double.parse(toStringAsFixed(n));
}

/// Usage:
void main() {
  double d = 2.3456789;
  double d1 = d.toPrecision(1); // 2.3
  double d2 = d.toPrecision(2); // 2.35
  double d3 = d.toPrecision(3); // 2.345
}
Posted by: Guest on January-14-2021

Code answers related to "flutter double round up by to 2 decimal places"

Browse Popular Code Answers by Language