Alvin
Alvin

Reputation: 8499

double.parse convert two zero decimal to one decimal

I have a string 10.00 and I want to convert it to double 10.00.

I use :

string str = "10.00";
double db = double.Parse(str);

the result I get is 10.0 and not 10.00.

Upvotes: 1

Views: 2860

Answers (3)

komizo
komizo

Reputation: 1061

The Parse and TryParse on the numeric respect local culture settings; you can change this by specifying a CultureInfo object. For instance, parsing 2.999 into a double gives 2999 in Germany:

Console.WriteLine (double.Parse ("2.999"));   // 2999 (In Germany) 

This is because in Germany, the period indicates a thousands separator rather than a decimal point. Specifying invariant culture fixes this:

double x = double.Parse ("2.999", CultureInfo.InvariantCulture);

The same when calling ToString():

string x = 2.9999.ToString (CultureInfo.InvariantCulture);

Upvotes: 5

loopedcode
loopedcode

Reputation: 4893

Question isn't really clear, but if you are referring to changing the double back to string with 2 decimal place precision, you can use:

string str = "10.00"
double db = double.parse(str);
string convertedBack = db.ToString("0.00");

Upvotes: 1

Adam Plocher
Adam Plocher

Reputation: 14233

A double isn't a string. If you want to display the double as a string, you can format it to have two decimal points.

For example:

string str = "10.00";
double db = double.Parse(str);
String.Format("{0:0.00}", db); // will show 10.00

Upvotes: 2

Related Questions