var num1 = 10.12345678

What should i do with num1 to delete digits after two decimal point without rounding its value.
I need output as 10.12

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      title: ' Delete digits after two decimal point ',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHome(),
    ));

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
    @override
  Widget build(BuildContext context) {

    var num1 = 10.12345678;
    print(num1); // I need output as 10.12

    return Container();
  }

}


Solution 1: Theo

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));


Solution 2: CopsOnRoad

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


Solution 3: Javier González

if you want round the number use this.

 double mod = pow(10.0, places);
 return ((val * mod).round().toDouble() / mod);

if you just want to truncate use this.

return val - val % 0.01;


Solution 4: Fodil

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

(n * 100).truncateToDouble()/100


Solution 5: Rakesh Lanjewar

String toFixed2DecimalPlaces(double data, {int decimalPlaces = 2}) {
List<String> values = data.toString().split('.');
if (values.length == 2 && values[0] != '0' && values[1].length >= decimalPlaces && decimalPlaces > 0)
return values[0] + '.' + values[1].substring(0, decimalPlaces);
else
return data.toString();
}


Solution 6: Rasel Khan

You can also try this ----> (0.2055).toStringAsFixed(2)

    var per = 0.2055;

    Text( "result view -> ${double.parse((per * 100).toStringAsFixed(2))}%",
            style: TextStyle(color: Colors.white, fontSize: 10)),

result value -> 
input -> 0.2055
output -> result view ->20.00 


Solution 7: Sohel Mahmud

There is a simple solution to this problem.

double value = 17.56565656;
//as string
String formatted = value.toStringAsFixed(2); // 17.56
//as double
double formattedDouble = double.parse(formatted); //17.56


Solution 8: Sabarish Chungath

extension NoRoundingDecimal on double {
String toDecimalAsFixed(int toDecimal) {
var right;
try {
right = this.toString().split(".")[1].padRight(toDecimal, "0").substring(0, toDecimal);
} catch (e) {
right = "00";
}
var left = this.toString().split(".")[0];

double number = double.parse(left + &quot;.&quot; + right);
return number.toStringAsFixed(toDecimal);

} }

Example1: double price = 71.999999999; print("number: ${price.toDecimalAsFixed(3)}");

Result: number: 71.999

Example2: double price = 71; print("number: ${price.toDecimalAsFixed(3)}");

Result: number: 71.000