Mark H
Mark H

Reputation: 727

parsing float into string

I have a number like so: 4.47778E+11

Can anyone give me a way of converting that into its number representation easily in c#?

Thanks

Upvotes: 0

Views: 1102

Answers (4)

yamspog
yamspog

Reputation: 18343

string s = "4.47778e+11";
double d = double.Parse(s);

or

string s = "4.47778e+11";
if (double.TryParse(s,out d))
{
    // number was parsed correctly
}

or for internationalization

double.Parse("4.47778e+11", System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture);

Upvotes: 3

Thomas Maierhofer
Thomas Maierhofer

Reputation: 2681

Use Convert:

double value = Convert.ToDouble("4.47778E+11");

Upvotes: 0

Wim
Wim

Reputation: 12082

Use

float num = Convert.ToFloat(Convert.ToDouble(s));

But you still lose precision, floats are only precise to 7 digits, so you're better off using just the Convert.ToDouble() (precise to 15 or so digits), so you won't lose any digits in your example.

Upvotes: 0

mike
mike

Reputation: 3166

Try this MSDN thread. It's called scientific notation by the way, and a quick google normally solves simple issues.

That's assuming you mean parsing from a string to a float, your question & title are conflicting

Upvotes: 0

Related Questions