Why does float.parse return wrong value?
Asked Answered
B

1

13

I have a problem. when I parse a string like "0.005" to float or double, it works fine on my computer, but when i install my program to my client's computer, it returns 5. (both my computer and my client's computer are using Windows 7 x64). Here are my examples

public float getFloat()
    {
        float mn = float.Parse("0.005");
        double mn2 = Convert.ToDouble("0.005");
        return mn;
    }
Behah answered 20/5, 2013 at 19:39 Comment(3)
Are you guys using the same culture in your machines? . is not the decimal separator in every culture. Edit: In PT-BR, for example, 0.005 IS 5.Landwaiter
that's an interesting broblem you have there.Bendwise
Yes, the problem is almost definitely that your client's computer is set to use "." as a thousands separator rather than a decimal point, as @Renan alludes to.Nabors
S
20

It could be problem with system culture settings. Try this:

float.Parse("0.005", CultureInfo.InvariantCulture);
Something answered 20/5, 2013 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.