在C#中,我可以这样做:
12341.4.ToString("##,#0.00")
,结果是12,345.40
飞镖中的等效物是什么?
答案 0 :(得分:19)
我也希望找到解决方案,并发现它现在按照以下示例实现。
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"
*/
}
答案 1 :(得分:8)
如果您不想打印货币符号:
import 'package:intl/intl.dart';
var noSimbolInUSFormat = new NumberFormat.currency(locale: "en_US",
symbol: "");
答案 2 :(得分:4)
以下是扑动实施的一个例子:
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,与添加到格式化结果的$无关。
答案 3 :(得分:1)
答案 4 :(得分:1)
我用它。它对我有用
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;
}
}
}
并在TextFormField
中onChanged: (text) {
priceController.text = moneyFormat.moneyFormat(priceController.text);
}
答案 5 :(得分:0)
感谢@Richard Morgan(回答 above)
这是我的最终解决方案。
注意:您需要这两个包才能使用其他货币。
此外,请检查您使用的数据类型以决定是否要使用 int
而不是 String
。如果 int
则无需在 int.parse()
format()
<块引用>
轻松查看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,
),
),