C# Convert string to double/decimal and back to string, keeping trailing zeroes, adding comas for thousands

Viewed 1360

I am trying to get user input, parse it and then display with String.Format(), formatting thousands with comas.

So, if user provides
1000  I will display 1,000
1000.00 => 1,000.00
1000.0  => 1,000.0
1,000.5 => 1,000.5

Basically I want to keep all decimals(including trailing zeroes) that were provided and just add formatting for thousands. I tried:

String.Format("{0:#,0.######}" , Decimal.Parse(input));
String.Format("{0:#,0.######}" , Double.Parse(input);
4 Answers
Related