For 10 I want 10 and not 10.00 For 10.11 I want 10.11
Is this possible without code? i.e. by specifying a format string alone simlar to {0:N2}
For 10 I want 10 and not 10.00 For 10.11 I want 10.11
Is this possible without code? i.e. by specifying a format string alone simlar to {0:N2}
This can be achieved using CultureInfo. Use the below using statement to import the library.
using System.Globalization;
For the decimal conversion, ## can be used for optional decimal places and 00 can be used for mandetory decimal places. Check the below examples
double d1 = 12.12;
Console.WriteLine("Double :" + d1.ToString("#,##0.##", new CultureInfo("en-US")));
String str= "12.09";
Console.WriteLine("String :" + Convert.ToDouble(str).ToString("#,##0.00", new CultureInfo("en-US")));
String str2 = "12.10";
Console.WriteLine("String2 with ## :" + Convert.ToDouble(str2).ToString("#,##0.##", new CultureInfo("en-US")));
Console.WriteLine("String2 with 00 :" + Convert.ToDouble(str2).ToString("#,##0.00", new CultureInfo("en-US")));
int integ = 2;
Console.WriteLine("Integer :" + Convert.ToDouble(integ).ToString("#,##0.00", new CultureInfo("en-US")));
the results are as follows
Double :12.12
String :12.09
String2 with ## :12.1
String2 with 00 :12.10
Integer :2.00