Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

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
Source by www.bezkoder.com #
 
PREVIOUS NEXT
Tagged: #dart #decimals
ADD COMMENT
Topic
Name
7+6 =