在C#中使用自定义千位分隔符

时间:2023-01-14 16:05:56

I'm trying not to use the ',' char as a thousand separator when displaying a string, but to use a space instead. I guess I need to define a custom culture, but I don't seem to get it right. Any pointers?

我试图在显示字符串时不使用','char作为千分隔符,而是使用空格代替。我想我需要定义一个自定义文化,但我似乎没有把它弄好。有什么指针吗?

eg: display 1000000 as 1 000 000 instead of 1,000,000

例如:显示1000000为1 000 000而不是1,000,000

(no, String.Replace() is not the solution I'd like to use :P)

(不,String.Replace()不是我想要使用的解决方案:P)

4 个解决方案

#1


I suggest you find a NumberFormatInfo which most closely matches what you want (i.e. it's right apart from the thousands separator), call Clone() on it and then set the NumberGroupSeparator property. (If you're going to format the numbers using currency formats, you need to change CurrencyGroupSeparator instead/as well.) Use that as the format info for your calls to string.Format etc, and you should be fine. For example:

我建议你找到一个与你想要的最匹配的NumberFormatInfo(即除了千位分隔符之外它是正确的),在它上面调用Clone()然后设置NumberGroupSeparator属性。 (如果您要使用货币格式格式化数字,则需要更改CurrencyGroupSeparator。)使用它作为调用string.Format等的格式信息,您应该没问题。例如:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        NumberFormatInfo nfi = (NumberFormatInfo)
            CultureInfo.InvariantCulture.NumberFormat.Clone();
        nfi.NumberGroupSeparator = " ";

        Console.WriteLine(12345.ToString("n", nfi)); // 12 345.00
    }
}

#2


Create your own NumberFormatInfo (derivative) with a different thousand separator.

用不同的千位分隔符创建自己的NumberFormatInfo(衍生物)。

#3


There's a slightly simpler version of Jon Skeet one :

有一个稍微简单的Jon Skeet版本:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        NumberFormatInfo nfi = new NumberFormatInfo {NumberGroupSeparator = " ", NumberDecimalDigits = 0};

        Console.WriteLine(12345678.ToString("n", nfi)); // 12 345 678
    }
}

And the 'nfi' initialization could be skipped and put directly as parameter into the ToString() method.

并且可以跳过'nfi'初始化并直接作为参数放入ToString()方法。

#4


Easiest way...

num.ToString("### ### ### ### ##0.00")

#1


I suggest you find a NumberFormatInfo which most closely matches what you want (i.e. it's right apart from the thousands separator), call Clone() on it and then set the NumberGroupSeparator property. (If you're going to format the numbers using currency formats, you need to change CurrencyGroupSeparator instead/as well.) Use that as the format info for your calls to string.Format etc, and you should be fine. For example:

我建议你找到一个与你想要的最匹配的NumberFormatInfo(即除了千位分隔符之外它是正确的),在它上面调用Clone()然后设置NumberGroupSeparator属性。 (如果您要使用货币格式格式化数字,则需要更改CurrencyGroupSeparator。)使用它作为调用string.Format等的格式信息,您应该没问题。例如:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        NumberFormatInfo nfi = (NumberFormatInfo)
            CultureInfo.InvariantCulture.NumberFormat.Clone();
        nfi.NumberGroupSeparator = " ";

        Console.WriteLine(12345.ToString("n", nfi)); // 12 345.00
    }
}

#2


Create your own NumberFormatInfo (derivative) with a different thousand separator.

用不同的千位分隔符创建自己的NumberFormatInfo(衍生物)。

#3


There's a slightly simpler version of Jon Skeet one :

有一个稍微简单的Jon Skeet版本:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        NumberFormatInfo nfi = new NumberFormatInfo {NumberGroupSeparator = " ", NumberDecimalDigits = 0};

        Console.WriteLine(12345678.ToString("n", nfi)); // 12 345 678
    }
}

And the 'nfi' initialization could be skipped and put directly as parameter into the ToString() method.

并且可以跳过'nfi'初始化并直接作为参数放入ToString()方法。

#4


Easiest way...

num.ToString("### ### ### ### ##0.00")