1

I am using the below code:

string.Format("{0:###,###,###,###,###.00}",12005557590928.143);

to convert the double value to string.

it gives the output as "12,005,557,590,928.10"

if I change it to

string.Format("{0:###,###,###,###,###.##}",12005557590928.143);

I get. "12,005,557,590,928.1".

How can I get the output as "12005557590928.143"?

Uthistran Selvaraj
  • 1,371
  • 1
  • 12
  • 31
  • 1
    Here is a link that will show you all the ways to use `string.Format` http://www.csharp-examples.net/string-format-double/ – MethodMan Jan 09 '13 at 15:54

5 Answers5

3

You're seeing a precision error because of the data type you're using for the number. Try using a fixed point number type (in this case, a decimal):

string.Format("{0:###,###,###,###,###.###}", 12005557590928.143m);
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • @Rotem - Interesting. MSDN used to refer to it as fixed point. They're documentation actually still says "Compared to floating-point...". – Justin Niessner Jan 09 '13 at 16:07
1

I believe you want string.Format("{0:###,###,###,###,###.000}",12005557590928.143); (note the extra zero at the end)

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
1

You can use

(12005557590928.143).ToString("R")

The custom numeric format strings will never reveal more than 15 decimal digits of a System.Double. Consider using System.Decimal if you need greater precision.

If your goal is to reveal "hidden" digits of the Double, use standard numeric format strings "R" ("round-trip") or "G17" ("general" 17 digits).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
1

Try with .ToString("R")

Console.WriteLine((12005557590928.143).ToString("R"));

Here is a DEMO.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1
Console.Write(string.Format("{0:0.000}", (12005557590928.143m)));
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
K Roobroeck
  • 1,378
  • 2
  • 10
  • 13
  • this yields `"12005557590928.143"` what about the commas ? – MethodMan Jan 09 '13 at 16:02
  • Can you please explain the suffix 'm' here? – Uthistran Selvaraj Jan 10 '13 at 03:55
  • @UthistranS. The 'm' stands for a decimal. For more information, Jon Skeet has a very good answer on the difference between float, double and decimal on stackoverflow http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c – K Roobroeck Jan 10 '13 at 09:36