【问题标题】:Currency format in dart飞镖中的货币格式
【发布时间】:2013-01-29 17:00:05
【问题描述】:

在 C# 中我可以做到:

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

结果是 12,345.40

dart 中的等价物是什么?

【问题讨论】:

标签: formatting dart


【解决方案1】:

我也想找到解决方案,发现现在按照以下示例实现。

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"  
*/
}

【讨论】:

  • 这是一种享受,甚至比也可用的 NumberFormat.currencyPattern() 格式化程序更好。
  • @Günter Zöchbauer 我怎样才能设置这个数字格式与货币符号为“it-IT”。
  • 这里是document link
  • @orlade 但是当货币小数点分隔符与普通小数点分隔符不同时,它应该会失败。您肯定想使用simpleCurrency() 函数来获得任何语言环境的正确格式。
  • 这个函数的轮次是错误的 我试过这个 oCcy.format(123456789.725) 结果是 123456789,72
【解决方案2】:

这是一个颤振实现的例子:

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。

注意Text('${...中的$只是引用''里面的变量_moneyCounter,与格式化结果中添加的$无关。

【讨论】:

  • 印度卢比怎么办?
  • _moneyCounter 是双倍的吗?
【解决方案3】:

如果您不想打印货币符号:

import 'package:intl/intl.dart';

var noSimbolInUSFormat = new NumberFormat.currency(locale: "en_US",
      symbol: "");

【讨论】:

  • 这个 NumberFormat("#,##0.00", "en_US"); 有什么不同和你的
  • 对于任何看到这个的西班牙人:var formatter = new NumberFormat.currency(locale: "es_ES", symbol: "");
【解决方案4】:

我是飞镖包money2的作者

https://pub.dev/packages/money2

该软件包支持固定精度数学、格式化和解析货币。

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

【讨论】:

  • 美包Brett,感谢创作和分享。这正是我想要的!文档也不错。
【解决方案5】:

我用它。它对我有用

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);
}

【讨论】:

  • 老实说,我不明白正则表达式是如何工作的。我有一个名为 price = '2845000.00' 的字符串。如何将其转换为 28,45,000?上面的代码给了我 28,450,000。非常感谢您的帮助。
  • 错误的正则表达式。这弄乱了小数点。
【解决方案6】:

感谢@Richard Morgan(回答above)

这是我的最终解决方案。

注意:您需要这两个包才能使用其他货币。 此外,请检查您使用的数据类型以确定是否要使用 int 而不是 String。如果int 则无需在format() 中使用int.parse()

方便查看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,
            ),
       ),
    

【讨论】:

    猜你喜欢
    • 2017-02-18
    • 2015-10-24
    • 2014-02-15
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 2018-04-01
    相关资源
    最近更新 更多