【问题标题】:String.Format Currency for K, M and BK、M 和 B 的 String.Format 货币
【发布时间】:2015-03-17 04:23:36
【问题描述】:

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

例如:

   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 紧跟在数字之后和货币符号之前。

似乎这一步走得太远了。有什么想法吗?

【问题讨论】:

  • 好的。知道了。你可以试试Unicodes 的货币。就像欧元一样,它是u20A0,并格式化您的return value。您将需要使用switch case 声明来处理不同的货币。

标签: c# currency string.format


【解决方案1】:

我会像这样使用 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") : 100 Kр.

【讨论】:

  • 我经常使用 IFormatProvider 来处理我想要实现的特殊字符串格式。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2019-04-14
    • 2011-05-21
    相关资源
    最近更新 更多