Convert string to a number

R

Rick

How do I convert a string value 5.3 to a number for comparision in vs2003
e.g
5.3 >= 5.1
3.9 <= 5.3
 
M

Morten Wennevik [C# MVP]

Rick said:
How do I convert a string value 5.3 to a number for comparision in vs2003
e.g
5.3 >= 5.1
3.9 <= 5.3

Hi Rick,

Use Convert.ToDouble, Convert.ToDecimal or Double.Parse/Double.TryParse,
Decimal.Parse/Decimal.TryParse

If you know the numbers are always valid, Double.Parse is probably easiest
to use, although many would prefer Convert.ToDouble.

double d = double.Parse("5.3");

or

double d = Convert.ToDouble("5.3");

In general the Convert class allows for conversions between many types,
whereas Parse has overloads that allow you to specify number styles and
formats.

// Convert Norwegian floating point numbers
double d = double.Parse("5,3", CultureInfo.GetCultureInfo("nb-NO"));

The TryParse methods are slightly more complicated to use but will never
throw an exception on illegal numbers

double d;
if(double.TryParse("not A Number", out d))
{
// valid number
total += d;
}
 
R

Rick

Peter,
This thread is about converting a string to a number; i have tried using
decimal or double conversions but got the Invocation errors. The other thread
was related to version class and I did not want to mix up these two
questions. I have also been following up with my other thread and posted a
reply to your question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top