format string c# 2 decimal places code example

Example 1: c# format string with 2 decimals

// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

Example 2: format double to 2 decimal places in c#

priceLbl.Text = price.ToString("0.##");

Example 3: C# decimal with two places store as string with two places

public static string DoFormat( double myNumber )
{
    var s = string.Format("{0:0.00}", myNumber);

    return s;
}

Example 4: c# format string with 2 decimals

// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567);      // "123.5"
String.Format("{0:00.0}", 23.4567);       // "23.5"
String.Format("{0:00.0}", 3.4567);        // "03.5"
String.Format("{0:00.0}", -3.4567);       // "-03.5"

Tags:

Java Example