3

I have a string with value "20.616378139" and when i try to convert using Convert.ToDouble or Double.Parse i get 20616378139.0 insted of the right value.

Why is this happening and how should I fix it?

4
  • 2
    Cultural settings where . is a thousands separator instead of decimal? Commented Oct 7, 2011 at 16:53
  • 1
    Could you please show us the code that produces the incorrect result? What you've described sounds very unusual. Commented Oct 7, 2011 at 16:54
  • @AnthonyPegram Hmm, good call... though in the example above it looks more like a billions-delimiter... :S Commented Oct 7, 2011 at 16:54
  • can you show us your code? although your question looks straightforward, just want to see if there is anything else going on. Commented Oct 7, 2011 at 16:56

3 Answers 3

16

You probably live in a part of the world where the decimal point is written as a comma. Fix:

var str = "20.616378139";
var dbl = double.Parse(str, System.Globalization.CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

Comments

-2

There's an overload to the Parse method that provides an options parameter of some kind; this is the way that you can specify for it to handle scientific notation, etc. Try setting that explicitly. If that works, then look at the default culture info settings you are using.

4 Comments

I'm not going to downvote you this time, but if you're going to post an answer you need to be more specific than "some kind of parameter".
@Parmenion - Agreed. Compare your answer to @Hans Passant's
Parmenion, you are overreacting. On StackOverflow, a vague answer is not as valuable as a precise answer. @JamesJohnson's comment was only intended to point that out, it was not a personal attack. Please read the FAQ and generally become more familiar with the intent (and community culture) of this website.
@Parmenion: We're all here helping people for free, but that doesn't mean every answer should get a standing ovation. I was trying to help you (as a new user) improve the quality of your answer. You could have revised your answer and gained some reputation, but you chose instead to take offense.
-2

I've used this command and there is no problem for me before.

string s = "20.616378139"; double d = Convert.ToDouble(s); ![enter image description here][1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.