C# C# Console

Format Decimal or Double Numbers into String in C#

In this tutorial, we will learn how to format decimal or double numbers into string in C#. String.Format converts the value of objects into strings based on the given formats.

String.Format converts the value of objects into strings based on the given formats. Below examples shows how to format double or decimal values to string in C#.

To format the numbers to fixed decimal places, you can use the “0” custom specifier as shown in the below example:

To format the numbers to a maximum of two decimal places, you can use the “#” custom specifier as shown in the below example:

To format the numbers to any minimum number of digits, you can use the “.” custom specifier as shown in the below example:

To format the numbers with thousands separator, you can use the “,” custom specifier as shown in the below example:

To  format positive, negative numbers and zero, you can use “;” custom specifier to split the pattern into 3 sections as shown in the below example:

To align numbers with spaces then use “,” before the colon and align numbers to the left use a negative number of spaces.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.