Uthistran Selvaraj
Uthistran Selvaraj

Reputation: 1371

double to string conversion

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"?

Upvotes: 1

Views: 272

Answers (5)

K Roobroeck
K Roobroeck

Reputation: 1418

Console.Write(string.Format("{0:0.000}", (12005557590928.143m)));

Upvotes: 1

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61952

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).

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245419

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);

Upvotes: 3

Soner Gönül
Soner Gönül

Reputation: 98750

Try with .ToString("R")

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

Here is a DEMO.

Upvotes: 1

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

Upvotes: 1

Related Questions