String.Format K、M 和 B 的货币

String.Format Currency for K, M and B

如果货币金额很大,我正在尝试缩写它。

例如:

   if (amt > 1000000)
   {
       decimal d = (decimal)Math.Round(amt / 1000, 0);
       return String.Format("{0:C0}", d) + " K";
   }

如果给定的数字超过 100 万,它将去掉最后 3 位数字并用 K 替换。当货币符号(如 $ 在左侧)时工作正常

但是,一些货币符号会放在右侧。

因此,我不会用漂亮的 $100 K 换取美元,而是用 100 € K 换取法国欧元。

如何更改格式以将 K 紧跟在数字之后、货币符号之前。

似乎有点过头了。有什么想法吗?

我会像这样使用 IFormatProvider 创建一个 class

public class MoneyFormat: IFormatProvider,  ICustomFormatter
        {
            public object GetFormat(Type formatType)
            {
                if (formatType == typeof(ICustomFormatter))
                    return this;
                else
                    return null;
            }

            public string Format(string fmt, object arg, IFormatProvider formatProvider)
            {
                if (arg.GetType() != typeof(decimal))
                    try
                    {
                        return HandleOtherFormats(fmt, arg);
                    }
                    catch (FormatException e)
                    {
                        throw new FormatException(string.Format("The format of '{0}' is invalid", fmt), e);
                    }

                string ufmt = fmt.ToUpper(CultureInfo.InvariantCulture);
                if (!(ufmt == "K"))
                    try
                    {
                        return HandleOtherFormats(fmt, arg);
                    }
                    catch (FormatException e)
                    {
                        throw new FormatException(string.Format("The format of '{0}' is invalid", fmt), e);
                    }

                decimal result;
                if (decimal.TryParse(arg.ToString(), out result))
                {
                    if (result >= 1000000)
                    {
                        decimal d = (decimal)Math.Round(result / 10000, 0);

                        CultureInfo clone = (CultureInfo)CultureInfo.CurrentCulture.Clone();
                        string oldCurrSymbol = clone.NumberFormat.CurrencySymbol;
                        clone.NumberFormat.CurrencySymbol = "";

                        return String.Format(clone, "{0:C0}", d).Trim() + " K" + oldCurrSymbol;
                    }
                }
                else
                    return string.Format("{0:C0}", result) + " K";
            }

            private string HandleOtherFormats(string format, object arg)
            {
                if (arg is IFormattable)
                    return ((IFormattable)arg).ToString(format, CultureInfo.CurrentCulture);
                else if (arg != null)
                    return arg.ToString();
                else
                    return string.Empty;
            }
}

然后你可以像这样以你的格式调用它:

return string.Format( new MoneyFormat(), "{0:K}", amt);

然后您可以调整您想要表示 "K" 或其他要添加的参考符号的方式

文化信息("fr-fr"):10 万欧元

文化信息("en-us"):100 千美元

文化信息("ru-RU") : 100 Kр.

您可以使用 CurrencyPositivePattern 来确定货币符号是在数字之前还是之后。然后您可以修改 CurrencySymbol 以满足您的需要。

        decimal amt = 10000000;
        Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");     //set up France as current culture
        NumberFormatInfo NFI = CultureInfo.CurrentCulture.NumberFormat;

        string currencySymbol =  NFI.CurrencySymbol;
        int currencyPosition = NFI.CurrencyPositivePattern;

        if (amt > 1000000)

        {
        if (currencyPosition == 3)     // n $  
        {
            NFI.CurrencySymbol = "K " + currencySymbol;
        }
            decimal d = (decimal)Math.Round(amt / 1000, 0);
            string output = d.ToString("c");
        }

我知道这不是自定义数字格式的最佳实现,但这只是为了让大家理解这个想法。

参见: NumberFormatInfo.CurrencyPositivePattern Property