【问题标题】:Number Format Patterns for Currency货币的数字格式模式
【发布时间】:2020-09-16 01:45:19
【问题描述】:

我已将此货币模式设置为基于值的符号(+ 或 -)前缀

static final String _currencyWithPrefixSignAndSymbol = "+ \u00A4 0.00 ;- \u00A4 0.00";

如果值为正,则该模式放一个 + 前缀,如果为负,则放一个 -;

问题:我需要在值为 0 时删除这些符号,有没有办法在模式中做到这一点?不做类似字符串的事情。最后替换?

【问题讨论】:

    标签: flutter dart number-formatting intl currency-formatting


    【解决方案1】:

    作为一种选择。
    从 Dart 2.7 开始,您可以创建扩展。 所以你可以像这样创建num 扩展:

    extension MyCurrencyFormat on num {
      static final _currencyWithPrefixSignAndSymbol = NumberFormat("+ \u00A4 0.00;- \u00A4 0.00");
      static final _currencyZero = NumberFormat("  \u00A4 0.00");
    
      String toCurrencyFormat() {
        return this == 0 ? _currencyZero.format(this) : _currencyWithPrefixSignAndSymbol.format(this);
      }
    }
    

    然后以这种方式在代码中使用它:

    var str1 = 42.toCurrencyFormat(); //  + USD 42.00
    var str2 = 0.toCurrencyFormat();  //  USD 0.00
    

    【讨论】:

    • 谢谢,这太酷了,我不知道这个扩展
    猜你喜欢
    • 2017-03-23
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2015-10-24
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多