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)))),
结果是 $# ,# # # . # # 或者 $4,100.00。
请注意,$in Text (’${ ... 只是引用’’中的变量 _ moneyCounter,与添加到格式化结果中的 $无关。
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
//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,
),
),