简体   繁体   English

如何用逗号格式化数字?

[英]How do I format a number with commas?

int a = 10000000;
a.ToString();

How do I make the output? 如何制作输出?

10,000,000 10,000,000

尝试将N0为无小数部分:

string formatted = a.ToString("N0"); // 10,000,000

You can also do String.Format: 您也可以执行String.Format:

int x = 100000;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x); 
//Will output: 100,000

If you have decimal, the same code will output 2 decimal places: 如果您使用小数,则相同的代码将输出2个小数位:

double x = 100000.2333;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x); 
//Will output: 100,000.23

To make comma instead of decimal use this: 要使用逗号而不是十进制,请使用以下命令:

double x = 100000.2333;
string y = string.Empty;
y = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:#,##0.##}", x);

a.ToString("N0")

另请参阅: MSDN中的标准数字格式字符串

A simpler String.Format option: 一个更简单的String.Format选项:

int a = 10000000;
String.Format("{0:n0}", a); //10,000,000

a.tostring(“ 00,000,000”)

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

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