在 C# 中使用逗号作为 1000 分隔符打印数字
首先,将数字设为字符串 -
string num = "1000000.8765";
现在,对小数点前后不同的数字进行不同的处理 -
string withoutDecimals = num.Substring(0, num.IndexOf("."));
string withDecimals = num.Substring(num.IndexOf("."));使用 ToString() 方法设置千位分隔符的格式 -
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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP