Search code examples
c#decimalstring-conversion

How do I convert my string?


I have a string that can be up to 9 characters long including an optional decimal point but all the others will be numbers. It could be "123456789" or "12.345678", for example.

What variable type should I convert it to so that I can use it in calculations?

And how do I do that?


Solution

  • float.Parse("12.345678");
    

    or

    float.Parse("12.345678", CultureInfo.InvariantCulture.NumberFormat);
    

    For avoiding these kind of outputs:

    1.524157875019e+16

    8.10000007371e-9

    For integers you can checkout this link also: https://msdn.microsoft.com/en-us/library/bb397679.aspx