Delete digits after two decimal point without rounding its value in flutter?

If you want to round the number:

var num1 = 10.12345678;
var num2 = double.parse(num1.toStringAsFixed(2)); // num2 = 10.12

If you do NOT want to round the number:

Create this method:

double getNumber(double input, {int precision = 2}) => 
  double.parse('$input'.substring(0, '$input'.indexOf('.') + precision + 1));

Usage:

var input = 113.39999999999999;
var output = getNumber(input, precision: 1); // 113.9
var output = getNumber(input, precision: 2); // 113.99
var output = getNumber(input, precision: 3); // 113.999

You can use intl package (https://pub.dartlang.org/packages/intl#-installing-tab-)

var num1 = 10.12345678;
var f = new NumberFormat("###.0#", "en_US");
print(f.format(num1));

Some answers here did not work (top answer is round, not truncate). here is a way:

(n * 100).truncateToDouble()/100

Tags:

Dart

Flutter