在C#中使用逗号作为1000分隔符打印数字
首先,将数字设置为字符串-
string num = "1000000.8765";
现在,对小数点前后的数字进行不同的处理-
string withoutDecimals = num.Substring(0, num.IndexOf("."));
string withDecimals = num.Substring(num.IndexOf("."));使用该ToString()方法设置1000个分隔符的格式-
ToString("#,##0")以下是显示带有逗号的数字作为1000个分隔符的完整代码-
示例
using System;
public class Program {
public static void Main() {
string num = "1000000.8765";
string withoutDecimals = num.Substring(0, num.IndexOf("."));
string withDecimals = num.Substring(num.IndexOf("."));
withoutDecimals = Convert.ToInt32(withoutDecimals).ToString("#,##0");
Console.WriteLine(withoutDecimals + withDecimals);
}
}输出结果
1,000,000.8765