将逗号千位分隔符添加到十进制 (.net)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1587778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add comma thousand separator to decimal (.net)
提问by maxp
I have a decimal number, say 1234.500.
我有一个十进制数,比如 1234.500。
I want to display it as 1,234.5.
我想将其显示为 1,234.5。
I'm currently converting it to a double to remove the trailing '0's.
我目前正在将其转换为 double 以删除尾随的“0”。
string.Format("{0:0,0}",1234.500)removes the decimal place, and other formatting options seem to use two decimal places regardless.
string.Format("{0:0,0}",1234.500)删除小数位,而其他格式选项似乎无论如何都使用两位小数。
Can anyone offer insight?
任何人都可以提供见解吗?
回答by Henk Holterman
You should use a custom formatting like #,##0.00
您应该使用自定义格式,例如 #,##0.00
string s = string.Format("{0:#,##0.00}", xx);
Will produce 1,234.50when xx = 1234.5M
forget about converting to double, that won't really help.
1,234.50当 xx = 1234.5M
忘记转换为时会产生double,那不会真正有帮助。
回答by rohancragg
Are you aware that .NET has built-in support for correctly formatting numbers according the the regional settings of each user of your application? It might be better to leverage the user's own regional settings (the .NET framework knows all the right settings already).
您是否知道 .NET 内置支持根据您的应用程序的每个用户的区域设置正确格式化数字?最好利用用户自己的区域设置(.NET 框架已经知道所有正确的设置)。
However, if you want to fix your application to format numbers in a particular regional setting, you can still leveage a particular locale of your choice and force .NET to use that as the basis of all formatting (not just numbers):
但是,如果您想修复您的应用程序以在特定区域设置中格式化数字,您仍然可以利用您选择的特定区域设置并强制 .NET 将其用作所有格式(不仅仅是数字)的基础:
using System.Globalization;
using System.Threading;
...
CultureInfo us = new CultureInfo("en-US");
and then
进而
Thread.CurrentThread.CurrentUICulture = us;
or just
要不就
Console.WriteLine(d.ToString("c", us));
You should be aware that the use of commas as a thousands separator is appropriate for UK and USA but it is not how thousands should be displayed in other countries
您应该知道使用逗号作为千位分隔符适用于英国和美国,但在其他国家/地区不应该如何显示千位
"one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space"
“一千二十五在美国显示为 1,025,在德国显示为 1.025。在瑞典,千位分隔符是一个空格”
MSDN has dedicated sectiondevoted to this topic which they call 'Globalization' (i.e. that's a good search term if ever you need to hunt down more detail). That page describes how the use of the pound sign works as a digit placeholder for removing the trailing zeros (as suggested by Henk Holterman in a previous comment).
MSDN 有专门讨论这个主题的部分,他们称之为“全球化”(即,如果您需要寻找更多细节,这是一个很好的搜索词)。该页面描述了如何使用磅符号作为删除尾随零的数字占位符(如 Henk Holterman 在之前的评论中所建议的)。
See also Custom Numeric Format Strings.
另请参阅自定义数字格式字符串。
回答by LukeH
Another alternative:
另一种选择:
decimal d = 1234.56m;
string s = d.ToString("N"); // 1,234.56
回答by junmats
Have you tried this?
你试过这个吗?
String.Format("{0:0,0.00}", 1234.560);
回答by Agung
try this !
尝试这个 !
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Try
TextBox1.Text = Format(CInt(TextBox1.Text), "#,#")
TextBox1.SelectionStart = TextBox1.Text.Length
Catch ex As Exception
End Try
End Sub
回答by Guillermo Gutiérrez
Try this:
尝试这个:
string myNumber = string.Format("{0:#,0.00}", 1234.500);
(I've seen this in RDLC reports)
(我在 RDLC 报告中看到了这一点)

