【问题标题】:Currency format for display显示的货币格式
【发布时间】:2011-06-18 01:25:01
【问题描述】:

有没有办法为一个国家/地区设置正确的货币表示形式?

示例 英国 - £127.54 荷兰 € 127,54- 美国 $127.54

等等。

需要考虑的一些事情,

  1. 货币符号

  2. 货币符号放置 -- 它可以 位于之前或之后 数字。

  3. 负数展示

【问题讨论】:

    标签: c# .net asp.net globalization currency


    【解决方案1】:

    试试Currency Format Specifier(“C”)。它会自动考虑当前的 UI 文化并相应地显示货币值。

    您可以将它与 String.Format 或重载的 ToString 方法一起用于数字类型。

    例如:

    decimal value = 12345.6789M; // Be sure to use Decimal for money values. Do not use IEEE-754 types such as float (System.Single) and double (System.Double) as they can only store approximate values.
    Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));
    
    Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
    
    Console.WriteLine(value.ToString("C3", CultureInfo.CreateSpecificCulture("da-DK")));
    
    // The example displays the following output on a system whose
    // current culture is English (United States):
    //       $12,345.68
    //       $12,345.679
    //       kr 12.345,679
    

    【讨论】:

    【解决方案2】:

    这种功能是内置的。

    使用小数时,您可以使用format string“C”或“c”。

    decimal dec = 123.00M;
    string uk = dec.ToString("C", new CultureInfo("en-GB")); // uk holds "£123.00"
    string us = dec.ToString("C", new CultureInfo("en-US")); // us holds "$123.00"
    

    【讨论】:

      【解决方案3】:

      您可以使用string.Format("{0:c}", value)

      参见此处:

      【讨论】:

      【解决方案4】:

      此代码-(将货币设置为 GB(Britain/UK/England/£) 然后打印一行。然后将货币设置为 US/$ 并打印一行)

      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB",false);         
      Console.WriteLine("bbbbbbb   {0:c}",4321.2);
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US",false);
      Console.WriteLine("bbbbbbb   {0:c}",4321.2);
      

      将显示-

      bbbbbbb   £4,321.20
      bbbbbbb   $4,321.20
      

      对于文化名称的列表,例如en-GB en-US e.t.c.
      http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.80).aspx

      【讨论】:

        【解决方案5】:

        获取给定数字并使用 .ToString("C",cu​​lture) 显示的问题在于,它有效地将金额更改为给定文化的默认货币。如果您有一个给定的金额,该金额的 ISO 货币代码,并且您想针对给定的文化显示它,我建议您只创建一个如下所示的小数扩展方法。这不会自动假定货币是该文化的默认货币:

        public static string ToFormattedCurrencyString(
            this decimal currencyAmount,
            string isoCurrencyCode,
        CultureInfo userCulture)
        {
            var userCurrencyCode = new RegionInfo(userCulture.Name).ISOCurrencySymbol;
        
            if (userCurrencyCode == isoCurrencyCode)
            {
                return currencyAmount.ToString("C", userCulture);
            }
        
            return string.Format(
                "{0} {1}", 
                isoCurrencyCode, 
                currencyAmount.ToString("N2", userCulture));
        }
        

        这将使用当地货币符号或带有金额的 ISO 货币代码 - 以更合适的为准。更多关于主题in this blog post

        【讨论】:

        • 在某些文化中,数字格式与货币格式不同,因此这不适用于任何文化。您宁愿接受格式随货币而变化,或者您只需要完全自己进行格式设置。
        【解决方案6】:
        public static string ToFormattedCurrencyString(
            this decimal currencyAmount,
            string isoCurrencyCode,
            CultureInfo userCulture)
        {
            var userCurrencyCode = new RegionInfo(userCulture.Name).ISOCurrencySymbol;
        
        if (userCurrencyCode == isoCurrencyCode)
        {
            return currencyAmount.ToString("C", userCulture);
        }
        
        return string.Format(
            "{0} {1}", 
            isoCurrencyCode, 
            currencyAmount.ToString("N2", userCulture));
        

        }

        【讨论】:

        • 与@jakejgordon 2 年前的回答完全相同。
        【解决方案7】:

        如果您只有货币符号和小数位数,您可以使用以下辅助函数,它尊重符号/金额顺序、分隔符等,只更改货币符号本身和要显示的小数位数到。

        public static string FormatCurrency(string currencySymbol, Decimal currency, int decPlaces)
        {
            NumberFormatInfo localFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
            localFormat.CurrencySymbol = currencySymbol;
            localFormat.CurrencyDecimalDigits = decPlaces;
            return currency.ToString("c", localFormat);
        }
        

        【讨论】:

          【解决方案8】:

          您可能根本无法使用文化方法,例如,当您希望拥有独立于任何文化或货币的特定格式时。甚至@jackjgordon 的方法也不可靠,因为某些文化的数字格式与货币格式不同,这使得这不一致。

          这是一个手动格式化程序,无需任何文化即可工作,因此您可以独立格式化您的数字。

          public static class CustomNumberFormatExtensions
          {
              static readonly StringBuilder formatTextBuilder = new StringBuilder();
          
              public static string ToCustomFormattedString(this decimal d, int decimalPrecision, string decimalPoint, string groupSeperator = "", int groupLength = 3, MidpointRounding rounding = MidpointRounding.AwayFromZero)
              {
                  lock (formatTextBuilder)
                  {
                      formatTextBuilder.Clear();
          
                      string rawDigits = Math.Round(d * (decimal)Math.Pow(10, decimalPrecision), 0, rounding).ToString();
                      rawDigits = rawDigits.PadLeft(decimalPrecision + 1, '0');
          
                      if (decimalPrecision > 0)
                      {
                          formatTextBuilder.Insert(0, rawDigits.Substring(rawDigits.Length - decimalPrecision));
                          rawDigits = rawDigits.Substring(0, rawDigits.Length - decimalPrecision);
                          formatTextBuilder.Insert(0, decimalPoint);
                      }
          
                      while (rawDigits.Length > groupLength)
                      {
                          formatTextBuilder.Insert(0, rawDigits.Substring(rawDigits.Length - groupLength));
                          rawDigits = rawDigits.Substring(0, rawDigits.Length - groupLength);
                          formatTextBuilder.Insert(0, groupSeperator);
                      }
          
                      return rawDigits + formatTextBuilder.ToString();
                  }
              }
          
              public static string ToCustomFormattedString(this double d, int decimalPrecision, string decimalSeperator, string groupSeperator = "", int groupLength = 3, MidpointRounding rounding = MidpointRounding.AwayFromZero)
              {
                  return ((decimal)d).ToCustomFormattedString(decimalPrecision, decimalSeperator, groupSeperator, groupLength, rounding);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多