简体   繁体   English

用逗号格式化大量

[英]Formatting a large number with commas

I need to format a number so that there is a comma seperating the thousands place for any number over and including 10000. eg 10000 becomes 10,000 but 9999 remaains as 9999. 我需要格式化数字,以便用逗号分隔任何数字(包括10000或10000以上)的千位。例如10000变为10,000,但9999变为9999。

I would like to do this using a format string as I do not want to have to test the data to see if what range it is in. 我想使用格式字符串来执行此操作,因为我不想测试数据以查看其是否在范围内。

Does anyone know how to do this? 有谁知道如何做到这一点?

A format string cannot behave differently for different values, so the best you can do is: 格式字符串对于不同的值不能有不同的行为,因此,您可以做的最好的事情是:

int n;

string s = n >= 10000 ? n.ToString("n0") : n.ToString("d");

(This will use the user's culture; pass a INumberFormatInfo / CultureInfo if a different culture is needed.) (这将使用用户的区域性;如果需要其他区域性,则传递INumberFormatInfo / CultureInfo 。)

MSDN: Standard and Custom Numeric Format Strings MSDN: 标准自定义数字格式字符串

num > 9999 ? num.ToString("N0", CultureInfo.InvariantCulture) : num.ToString();

"N0" assuming you don't want decimals. 假设您不想要小数,则为“ N0”。 NFormat N格式

I would suggest creating your own IFormatProvider that has a quick length check and formats it with the thousands separator normally if it's five or more characters, or without the thousands separator if it's four or less characters. 我建议创建自己的IFormatProvider,它具有快速的长度检查功能,并且如果它是五个或更多字符,则通常使用千位分隔符对其进行格式化,如果它是四个或更少字符,则不使用千位分隔符对其进行格式化。

You could easily modify an example from the MSDN docs on IFormatProvider . 您可以轻松地从IFormatProvider上MSDN文档修改示例。

There has to be a range check at some level. 必须在某种程度上进行范围检查。 If you implement it like this, you can embed the range checking within the formatting framework to some extent, bypassing the default formatting (which includes group separators) if the number is fewer than 5 digits: 如果以这种方式实现,则可以在一定程度上将范围检查嵌入格式框架中,如果数量少于5位,则绕过默认格式(包括组分隔符):

class MyFormat : System.IFormatProvider, ICustomFormatter
{
    #region IFormatProvider Members

    public object GetFormat(Type formatType)
    {
        return this;
    }

    #endregion


    #region ICustomFormatter Members

    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        if (arg is double)
            if (((double)arg >= 10000) || ((double)arg <= -10000))
                return ((double)arg).ToString(format);
            else
                return ((double)arg).ToString("R");

        if (arg is decimal)
            if (((decimal)arg >= 10000) || ((decimal)arg <= -10000))
                return ((decimal)arg).ToString(format);
            else
                return ((decimal)arg).ToString("R");

        return arg.ToString();
    }

    #endregion
}

class Program
{
    static MyFormat gFormat = new MyFormat();

    static void Main(string[] args)
    {

        double dblVal1 = 9999, dblVal2 = 123456;
        Console.WriteLine(String.Format(gFormat, "{0:N0} {1:N0}", dblVal1, dblVal2));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM