Thursday, October 22, 2009

C# string formation

// two decimal places
String.Format("{0:0.00}", 324.4567); // "324.46"
String.Format("{0:0.00}", 324.4); // "324.40"
String.Format("{0:0.00}", 324.0); // "324.00"

// maximum two decimal places
String.Format("{0:0.##}", 324.4567); // "324.46"
String.Format("{0:0.##}", 324.4); // "324.4"
String.Format("{0:0.##}", 324.0); // "324"

// at least 2 digits before decimal point
String.Format("{0:00.0}", 324.4567); // "324.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"

No comments: