Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency format in dart

In C# I can do:

12341.4.ToString("##,#0.00") 

and the result is 12,345.40

What's the equivalent in dart?

like image 465
user2070369 Avatar asked Feb 14 '13 00:02

user2070369


People also ask

How do you format numbers as thousands separators in flutter?

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


2 Answers

I wanted to find the solution also, and found that it is now implemented as per following example.

import 'package:intl/intl.dart';  final oCcy = new NumberFormat("#,##0.00", "en_US");  void main () {    print("Eg. 1: ${oCcy.format(123456789.75)}");   print("Eg. 2: ${oCcy.format(.7)}");   print("Eg. 3: ${oCcy.format(12345678975/100)}");   print("Eg. 4: ${oCcy.format(int.parse('12345678975')/100)}");   print("Eg. 5: ${oCcy.format(double.parse('123456789.75'))}");  /* Output :    Eg. 1: 123,456,789.75 Eg. 2: 0.70 Eg. 3: 123,456,789.75 Eg. 4: 123,456,789.75 Eg. 5: 123,456,789.75    pubspec.yaml :    name: testCcy002   version: 0.0.1   author: BOH   description: Test Currency Format from intl.   dev_dependencies:     intl: any     Run pub install to install "intl"   */ } 
like image 173
Brian Oh Avatar answered Oct 07 '22 07:10

Brian Oh


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.format(_moneyCounter)}',                   style: new TextStyle(                     color: Colors.greenAccent,                     fontSize: 46.9,                     fontWeight: FontWeight.w800)))), 

Results in $#,###.## or $4,100.00 for example.

Note that the $ in Text('${... is only to reference the variable _moneyCounter inside the ' ' and has nothing to do with the $ added to the formatted result.

like image 26
Richard Morgan Avatar answered Oct 07 '22 06:10

Richard Morgan