TechieClues TechieClues
Updated date May 20, 2024
In this blog, we will see 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.
  • 25.4k
  • 0
  • 0

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:

string.Format("{0:0.00}", 1234.568)    // 1234.57
string.Format("{0:0.00}", 1234.562)    // 1234.56            
string.Format("{0:0.00}", 1234.5)      // 1234.50
string.Format("{0:0.00}", 1234.0)      // 1234.00       

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

string.Format("{0:0.##}", 1234.568)    // 1234.57
string.Format("{0:0.##}", 1234.562)    // 1234.56            
string.Format("{0:0.##}", 1234.5)      // 1234.5
string.Format("{0:0.##}", 1234.0)      // 1234

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

string.Format("{0:0.0}", 1234.568)    // 1234.6                      
string.Format("{0:0.0}", 1234.5)      // 1234.5
string.Format("{0:0.0}", 1234.0)      // 1234.0

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

string.Format("{0:0,0.000}", 12345678.901)    // 1,23,45,678.901          
string.Format("{0:0,0.00}", 12345678.901)     // 1,23,45,678.90     
string.Format("{0:0,0}", 12345678.901)        // 1,23,45,679 

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:

string.Format("{0:0.00}", 123456.789)    // 123456.79
string.Format("{0:0.00}", -123456.789)   // -123456.79

// {0:[positive];[negative];[zero]}
string.Format("{0:0.00;'negative: '-0.00;zero}", 123456.789)      // 123456.79
string.Format("{0:0.00;'negative: '-0.00;zero}", -123456.789)     // negative: -123456.79
string.Format("{0:0.00;'negative: '-0.00;zero}", 0.0)             // Zero

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

// Postitive values
string.Format("{0,10:0.0}", 123456.789)       //"    123456.8"
string.Format("{0,10:0.0}", -123456.789)      //"   -123456.8"
// Negative values
string.Format("{0,-10:0.0}", 123456.789)      //"123456.8    "
string.Format("{0,-10:0.0}", -123456.789)     //"-123456.8    "

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!