Answers for "flutter double round two decimal places"

5

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
0

round off double in flutter

import 'dart:math';

double roundDouble(double val, int places){
  num mod = pow(10.0, places);
  return ((val * mod).round().toDouble() / mod);
}
Posted by: Guest on December-15-2021

Code answers related to "flutter double round two decimal places"

Code answers related to "Dart"

Browse Popular Code Answers by Language