String.Format สำหรับ Double
ใช้ String.Format หรือ string.Format หรือ String Interpolation ($"") เพื่อจัดรูปแบบตัวเลขทศนิยม
รูปแบบ Format Specifiers สำหรับ Double
| Format Specifier | ชื่อ | ตัวอย่าง (1234.5678) | คำอธิบาย |
|---|---|---|---|
F / f |
Fixed-point | F2 → 1234.57 |
จำนวนทศนิยมคงที่ ปัดเศษ |
N / n |
Number | N2 → 1,234.57 |
มี Thousand Separator + ทศนิยมคงที่ |
E / e |
Scientific | E2 → 1.23E+003 |
Scientific Notation |
G / g |
General | G5 → 1234.6 |
General Format (Default) |
C / c |
Currency | C2 → $1,234.57 |
รูปแบบสกุลเงิน (ขึ้นกับ Culture) |
P / p |
Percent | P2 → 123,456.78% |
คูณ 100 + % Sign |
0.000 |
Custom | 0.000 → 1234.568 |
Custom Format - 0 = Digit บังคับ, # = Digit ไม่บังคับ |
ตัวอย่าง Custom Format Patterns
double value = 987.383;
double value2 = 987.38;
double value3 = 987.7899;
double bigNumber = 54567.46;
Console.WriteLine("=== ทศนิยม 3 ตำแหน่ง (0.000) ===");
Console.WriteLine(String.Format("{0:0.000}", value)); // 987.383
Console.WriteLine(String.Format("{0:0.000}", value2)); // 987.380 (เติม 0)
Console.WriteLine(String.Format("{0:0.000}", value3)); // 987.790 (ปัดเศษ)
Console.WriteLine("\n=== Thousands Separator ===");
Console.WriteLine(String.Format("{0:0,0.0}", bigNumber)); // 54,567.5
Console.WriteLine(String.Format("{0:0,0}", bigNumber)); // 54,567 (ไม่มีทศนิยม)
Console.WriteLine(String.Format("{0:N2}", bigNumber)); // 54,567.46 (Standard N Format)
Console.WriteLine("\n=== รูปแบบอื่นๆ ===");
Console.WriteLine(String.Format("{0:F2}", value)); // 987.38 (Fixed 2 decimal)
Console.WriteLine(String.Format("{0:C2}", value)); // $987.38 (Currency)
Console.WriteLine(String.Format("{0:E2}", value)); // 9.87E+002 (Scientific)
Console.WriteLine(String.Format("{0:P2}", 0.1234)); // 12.34% (Percent)
โค้ดตัวอย่างสมบูรณ์
using System;
class Demo {
public static void Main(String[] args) {
Console.WriteLine("=== ทศนิยม 3 ตำแหน่ง ===");
Console.WriteLine(String.Format("{0:0.000}", 987.383));
Console.WriteLine(String.Format("{0:0.000}", 987.38));
Console.WriteLine(String.Format("{0:0.000}", 987.7899));
Console.WriteLine("\n=== Thousands Separator ===");
Console.WriteLine(String.Format("{0:0,0.0}", 54567.46));
Console.WriteLine(String.Format("{0:0,0}", 54567.46));
Console.WriteLine("\n=== String Interpolation (C# 6+) ===");
double d = 1234.5678;
Console.WriteLine($"Fixed 2: {d:F2}");
Console.WriteLine($"Number 2: {d:N2}");
Console.WriteLine($"Currency: {d:C2}");
Console.WriteLine($"Custom: {d:0,0.00}");
}
}
String Interpolation (แนะนำใน C# 6.0+)
อ่านง่ายกว่า String.Format มาก
double price = 1234.56;
string name = "Product";
// แทน String.Format("{0:C2} - {1}", price, name)
string formatted = $"{price:C2} - {name}"; // $1,234.56 - Product
// Complex Formatting
Console.WriteLine($"Value: {1234.5:0,0.00}"); // Value: 1,234.50
Console.WriteLine($"Percent: {0.1234:P1}"); // Percent: 12.3%
CultureInfo (วัฒนธรรม) - สำคัญสำหรับ Production
using System.Globalization;
double value = 1234.56;
// US Culture (Default บน US Machine)
Console.WriteLine(value.ToString("C", new CultureInfo("en-US"))); // $1,234.56
// Thai Culture
Console.WriteLine(value.ToString("C", new CultureInfo("th-TH"))); // ฿1,234.56
// Invariant Culture (สำหรับ Save/Parse/Log)
Console.WriteLine(value.ToString("F2", CultureInfo.InvariantCulture)); // 1234.56
⚠️ ข้อควรระวัง
0= Digit บังคับ (จะแสดง 0 ถ้าไม่มีหลักนั้น)#= Digit ไม่บังคับ (จะไม่แสดงถ้าไม่มีหลักนั้น)String.Format("{0:0.00}", 1.2)→ "1.20" (เติม 0)String.Format("{0:#.##}", 1.2)→ "1.2" (ไม่เติม 0)- ใช้
CultureInfo.InvariantCultureสำหรับ Log/File/Network