【问题标题】:String.Format currencies fractional partString.Format 货币小数部分
【发布时间】:2013-11-17 16:09:56
【问题描述】:

我在这里和其他网站上进行了很多搜索,但我还没有完全找到我正在寻找的确切解决方案。

我想根据当前文化输出价格。我进行了以下测试:

decimal price = 909865747.98M;
decimal price2 = 90986574798M;

string frFR = string.Format(new System.Globalization.CultureInfo("fr-FR"), "{0:C}", price);
string frFR2 = string.Format(new System.Globalization.CultureInfo("fr-FR"), "{0:C}", price2);
string co = string.Format(new System.Globalization.CultureInfo("es-CO"), "{0:C}", price);
string co2 = string.Format(new System.Globalization.CultureInfo("es-CO"), "{0:C}", price2);

我得到了以下结果

frFR: 909 865 747,98 €
frFR2: 90 986 574 798,00 €
co: $909.865.747,98
co2: $90.986.574.798,00

如果十进制数实际上是整数,我不想得到小数部分。但我想保留文化提供的小数分隔符

有什么想法吗?

先谢谢了。

【问题讨论】:

    标签: c# format currency


    【解决方案1】:

    通过简单的检查decimalvalue % 1 == 0检查你的小数是否真的是一个整数或是否有数字

    如果是整数,则使用不带数字的格式字符串:{0:C0}

    string formatPrice = price % 1 == 0 ? "{0:C0}" : "{0:C}";
    string formatPrice2 = price2 % 1 == 0 ? "{0:C0}" : "{0:C}";
    
    string frFR = string.Format(new System.Globalization.CultureInfo("fr-FR"), formatPrice, price);
    string frFR2 = string.Format(new System.Globalization.CultureInfo("fr-FR"), formatPrice2, price2);
    

    【讨论】:

    • 这可能是一个解决方案,但在每个价格输出上都这样做有点矫枉过正......
    • 只需给自己写一个辅助函数就可以了?
    【解决方案2】:

    尝试使用 (custom numeric formats) 你的情况是:

    string frFR = string.Format(new System.Globalization.CultureInfo("fr-FR"),
                (new CultureInfo("fr-FR")).NumberFormat.CurrencyPositivePattern % 2 == 0 ?
                       "{0}{1:#,##0.##}" : "{1:#,##0.##}{0}", 
                new RegionInfo(new CultureInfo("fr-FR").LCID).CurrencySymbol, price);
    string frFR2 = string.Format(new System.Globalization.CultureInfo("fr-FR"), 
                (new CultureInfo("fr-FR")).NumberFormat.CurrencyPositivePattern % 2 == 0 ?
                       "{0}{1:#,##0.##}" : "{1:#,##0.##}{0}",
                new RegionInfo(new CultureInfo("fr-FR").LCID).CurrencySymbol, price2);
    
    string co = string.Format(new System.Globalization.CultureInfo("es-CO"),
                (new CultureInfo("es-CO")).NumberFormat.CurrencyPositivePattern % 2 == 0 ? 
                       "{0}{1:#,##0.##}" : "{1:#,##0.##}{0}",
                new RegionInfo(new CultureInfo("es-CO").LCID).CurrencySymbol, price);
    string co2 = string.Format(new System.Globalization.CultureInfo("es-CO"),
                (new CultureInfo("es-CO")).NumberFormat.CurrencyPositivePattern % 2 == 0 ? 
                       "{0}{1:#,##0.##}" : "{1:#,##0.##}{0}",
                new RegionInfo(new CultureInfo("es-CO").LCID).CurrencySymbol, price2);
    

    如果您想要 ISO 标准货币代码而不是符号,请使用 ISOCurrencySymbol 而不是 CurrencySymbol

    这是一种方法

    public static string FormatCurrency(decimal amount, string culture, 
           bool useIsoCurrencySymbol = false)
    {
       var ci = new CultureInfo(culture);
       return string.Format ci,
            ci.NumberFormat.CurrencyPositivePattern % 2 == 0 ? 
               "{0}{1:#,##0.##}" : "{1:#,##0.##}{0}",
            useIsoCurrencySymbol?
               new RegionInfo(ci.LCID).ISOCurrencySymbol:
               new RegionInfo(ci.LCID).CurrencySymbol, amount);
    }
    

    注意:“fr-FR”的千位分隔符显然是一个空格,而不是句点或逗号。

    【讨论】:

    • 我添加了当前文化货币符号
    • 谢谢。但这并没有这样做;)确实,货币有时位于价格的开头,有时位于价格的末尾……根据国家/地区。使用这种格式,你不会覆盖小数点分隔符吗?
    • 这仍然无法正常工作,因为您使用的是当前文化而不是明确指定的文化,并且您没有正确放置货币符号。例如,我得到“EUR909 865 747,98”;)我认为自定义数字格式很酷,但是由于数字和符号的放置顺序等原因,您不想将其用于货币...还有点和逗号在某些文化中的不同含义..
    • 如果您阅读了我提供的链接中的部分,它会说格式中的小数点代表小数点分隔符,并将替换为任何文化小数点分隔符。 ““.”自定义格式说明符将本地化的小数分隔符插入到结果字符串中”。至于逗号,就不是很清楚了。我已经编辑使用指定的文化,而不是当前的。
    • 我想我知道如何在正确的位置获取货币符号但是我还没有测试过...看看它是否有效
    【解决方案3】:

    你可以这样做:

    System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("fr-FR");
    List<decimal> lst = new List<decimal>()
    {
        909865747.98M,
        90986574798M
    };
    
    List<string> results = new List<string>();
    
    foreach(decimal d in lst)
    {
         if(d.Equals(decimal.Truncate(d)))
             results.Add(d.ToString("C0",ci));
         else
             results.Add(d.ToString("C",ci));
    }
    

    或者如果你想要一个字典已经包含你想要的cultureinfo对象的列表,在List&lt;decimal&gt;之后替换为:

    Dictionary<CultureInfo, List<string>> results = new Dictionary<CultureInfo, List<string>>()
    {
         {new System.Globalization.CultureInfo("fr-FR"),new List<string>()},
         {new System.Globalization.CultureInfo("es-CO"),new List<string>()}
    };
    
    foreach(decimal d in lst)
    {
          if (d.Equals(decimal.Truncate(d)))
          {
               foreach (var item in results)
               {
                   item.Value.Add(d.ToString("C0", item.Key));
               }
          }
          else
          {
               foreach (var item in results)
               {
                   item.Value.Add(d.ToString("C", item.Key));
               }
          }
    }
    

    如果是单排:

    System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("fr-FR");
    string frFR2 = price2.Equals(decimal.Truncate(price2)) ? price2.ToString("C0",ci) : price2.ToString("C",ci);
    string frFR = price.Equals(decimal.Truncate(price)) ? price.ToString("C0",ci) : price.ToString("C",ci);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      相关资源
      最近更新 更多