Mastering Thousands Separators in C# with String.Format()
The .NET framework provides a straightforward way to format numbers with thousands separators using the String.Format()
method. This powerful method allows precise control over string formatting.
To add a comma as a thousands separator, utilize the "n" format specifier. Here's how it works:
$"{1234:n}"
: This expression formats the number 1234, adding a thousands separator and displaying two decimal places, resulting in "1,234.00".$"{9876:n0}"
: This example demonstrates formatting without decimal places, yielding "9,876". The 0
after the n
specifies zero decimal places.This technique offers a concise and efficient solution for incorporating thousands separators into your C# applications.
The above is the detailed content of How to Format Numbers with Thousands Separators using C# String.Format()?. For more information, please follow other related articles on the PHP Chinese website!