使用C#中的自定义千位分隔符 [英] Use a custom thousand separator in C#

查看:471
本文介绍了使用C#中的自定义千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不显示字符串时,使用','字符作为千位分隔符,而是使用空格来代替。我想我需要定义一个自定义的文化,但我似乎并没有得到它的权利。任何指针?

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?

例如:显示器百万为1 000 000,而不是100万

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

(不,与string.replace()是解决不了问题,我想使用方法:P)

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

推荐答案

我建议你找一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx\"><$c$c>NumberFormatInfo这最符合你想要什么(即这是正确的除了千位分隔符),呼叫<一个href=\"http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.clone.aspx\"><$c$c>Clone()它,然后设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupseparator.aspx\"><$c$c>NumberGroupSeparator属性。 (如果你打算使用货币格式来格式化数字,你需要改变<$c$c>CurrencyGroupSeparator而不是/为好。)使用它作为您的来电格式信息,以的String.Format 等,你应该罚款。例如:

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:

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
    }
}

这篇关于使用C#中的自定义千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆