Currency format in dart
FormattingDartFormatting Problem Overview
In C# I can do:
12341.4.ToString("##,#0.00")
and the result is 12,345.40
What's the equivalent in dart?
Formatting Solutions
Solution 1 - Formatting
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"
*/
}
Solution 2 - Formatting
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.
Solution 3 - Formatting
If you don't wanna print currency symbol:
import 'package:intl/intl.dart';
var noSimbolInUSFormat = new NumberFormat.currency(locale: "en_US",
symbol: "");
Solution 4 - Formatting
I'm the author of the dart package money2
https://pub.dev/packages/money2
The package supports fixed precision maths, formatting and parsing of money.
import 'money2.dart';
Currency usdCurrency = Currency.create('USD', 2);
// Create money from an int.
Money costPrice = Money.fromInt(1000, usdCurrency);
print(costPrice.toString());
> $10.00
final taxInclusive = costPrice * 1.1;
print(taxInclusive.toString())
> $11.00
print(taxInclusive.format('SCC #.00'));
> $US 11.00
// Create money from an String using the `Currency` instance.
Money parsed = usdCurrency.parse(r'$10.00');
print(parsed.format('SCCC 0.0'));
> $USD 10.00
// Create money from an int which contains the MajorUnit (e.g dollars)
Money buyPrice = Money.from(10);
print(buyPrice.toString());
> $10.00
// Create money from a double which contains Major and Minor units (e.g. dollars and cents)
// We don't recommend transporting money as a double as you will get rounding errors.
Money sellPrice = Money.from(10.50);
print(sellPrice.toString());
> $10.50
Solution 5 - Formatting
I use it. it's working for me
class MoneyFormat {
String price;
String moneyFormat(String price) {
if (price.length > 2) {
var value = price;
value = value.replaceAll(RegExp(r'\D'), '');
value = value.replaceAll(RegExp(r'\B(?=(\d{3})+(?!\d))'), ',');
return value;
}
}
}
and in TextFormField
onChanged: (text) {
priceController.text = moneyFormat.moneyFormat(priceController.text);
}
Solution 6 - Formatting
Thanks to @Richard Morgan (answer above)
This was my final solution.
Note: You need the two packages for you to use other currencies.
Also, check what type of data you using for the amount to decide if you want to use int
instead of String
. If int
then no need to use int.parse()
in the format()
> To easily check print(amount.runtimeType);
//packages
import 'dart:io';
import 'package:intl/intl.dart';
final formatCurrency = NumberFormat.simpleCurrency(
locale: Platform.localeName, name: 'NGN');
final int amount;
// or final String amount; if gotten from json file
// check what type of data with - print(data.runtimeType);
//generate your constructors for final fields
Text(
'${formatCurrency.format(amount)}',
// or for String '${formatCurrency.format(int.parse(amount))}'
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
Solution 7 - Formatting
import 'package:intl/intl.dart';
extension DoubleExt on double {
String toEuro() {
return NumberFormat.simpleCurrency(
name: 'EUR',
).format(this / 100);
}
String toPln() {
return NumberFormat.simpleCurrency(
name: 'PLN',
).format(this / 100);
}
}
just use it on your double. 1000.0.toEuro()
=> €10.00