converting from string to float problem

  • Thread starter Thread starter ool
  • Start date Start date
O

ool

Hello,

How I can convert value from string to double.

When I do this in that way:

String sParam="150.00";
double dbValue= Convert.ToDouble(szParam);

I get System.FormatEcxeption

Thomas
 
Thomas,

You might find the either Double.Parse or Double.TryParse to be a bit more
useful than Convert.ToDouble.

HTH,
Nicole
 
Hi Thomas,

ool said:
Hello,

How I can convert value from string to double.

When I do this in that way:

String sParam="150.00";
double dbValue= Convert.ToDouble(szParam);

I get System.FormatEcxeption

A hunch.

I note that your post seems to originate from Poland, yes? According to
Windows, the Polish culture uses a comma to seperate whole part from decimal
part. Assuming the number you are trying to parse follows the English (U.S.)
format, you could create a CultureInfo object and pass it as the second
(IFormatProvider) parameter to the Convert.ToDouble method.

System.Globalization.CultureInfo enUSCulture =
new System.Globalization.CultureInfo("en-US");

String sParam = "150.00";
double dbValue = Convert.ToDouble(szParam, enUSCulture);

Regards,
Dan
 
ool said:
How I can convert value from string to double.

When I do this in that way:

String sParam="150.00";
double dbValue= Convert.ToDouble(szParam);

I presume your actual code has the same variable name in both places.
I get System.FormatEcxeption

My guess is that you're in a culture where the decimal point is a comma
rather than a dot. Try

double dbValue = Convert.ToDouble (szParam,
CultureInfo.InvariantCulture);

(with a using System.Globalization; statement at the top of your code).
 
Back
Top