Tuesday, May 26, 2009

String Format C#.NET

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"


max. two decimal places
String.Format("{0:0.##}", 123.4567);              // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"


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"



Thousand separator

String.Format("{0:0,0.0}", 12345.67); // "12,345.7"
String.Format("{0:0,0}", 12345.67); // "12,346"


%
String.Format("{0:00%}", 12.3); //12.30%

String.Format("C{0:000000}", 1) //C000001


Post a Comment