Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart - NumberFormat

Tags:

Is there a way with NumberFormat to display :

  • '15' if double value is 15.00
  • '15.50' if double value is 15.50

Thanks for your help.

like image 742
matth3o Avatar asked Oct 10 '16 12:10

matth3o


People also ask

How do you display currency in flutter?

Here's an example from a flutter implementation: import 'package:intl/intl. dart'; final formatCurrency = new NumberFormat. simpleCurrency(); new Expanded( child: new Center( child: new Text('${formatCurrency.

How do you format numbers as thousands separators in flutter?

Use double. tryParse("10000") or double. parse("10000") , after this just format.

What is Intl NumberFormat?

The Intl. NumberFormat() object enables language-sensitive number formatting. We can use this to convert currency, numbers, percentages, units, and other notation into formatted strings. This is particularly useful in eCommerce applications, with examples like displaying an item price or recipt printing.


1 Answers

Actually, I think it's easier to go with truncateToDouble() and toStringAsFixed() and not use NumberFormat at all:

n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2); 

So for example:

main() {   double n1 = 15.00;   double n2 = 15.50;    print(format(n1));   print(format(n2)); }  String format(double n) {   return n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2); } 

Prints to console:

15 15.50 
like image 100
martin Avatar answered Sep 22 '22 16:09

martin