在 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