Use a custom thousand separator in C#(在 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?
例如:将 1000000 显示为 1 000 000 而不是 1,000,000
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)
推荐答案
建议你找一个NumberFormatInfo 最接近你想要的(即它与千位分隔符分开),调用 Clone() 就可以了,然后设置 NumberGroupSeparator 属性.(如果您要使用货币格式格式化数字,您需要更改 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# 中使用自定义千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# 中使用自定义千位分隔符
基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
