K,M和B的String.Format货币

时间:2015-03-17 04:23:36

标签: c# currency string.format

如果货币金额非常大,我正试图缩写它。

例如:

   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.

似乎这可能是一个太过分的步骤。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

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

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”或您想要添加的其他参考符号的方式

CultureInfo(“fr-fr”):100 K€

CultureInfo(“en-us”):100 K $

CultureInfo(“ru-RU”):100Kр。

答案 1 :(得分:1)

您可以使用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