0

I'm doing a system on C# and everything is going ok, but I'm having some troubles when I want convert a textbox value to double, for example: when I enter 123.40 the result of convert this to Double is 12340. Here is my code:

Double.Parse(txtPay.Text)

And if I try with:

Convert.ToDouble(txtPay.Text)

the result is the same

Prescott
  • 7,312
  • 5
  • 49
  • 70
Pepe
  • 27
  • 1
  • 1
  • 7
  • 3
    What is your system locale? I suspect that's the problem... – Jon Skeet Jul 27 '14 at 20:39
  • The laguage is Spanish(Mexico), Can this configuration be a problem? I'm frustred it's only one problem to continue with my system – Pepe Jul 27 '14 at 20:43
  • It's not a problem. Just see @dcastro answer below. – fourpastmidnight Jul 27 '14 at 20:45
  • I'm wondering how many times this question will be asked – Selman Genç Jul 27 '14 at 20:46
  • [1](http://stackoverflow.com/questions/11455306/why-double-parse0-05-returns-5-0) , [2](http://stackoverflow.com/questions/23040557/double-parse-doesnt-work-with-some-country-apparently) , [3](http://stackoverflow.com/questions/11399439/converting-string-to-double-in-c-sharp) , [4](http://stackoverflow.com/questions/12426978/c-sharp-convert-string-to-double-in-locale), [5](http://stackoverflow.com/questions/5056554/convert-string-to-double-c-sharp) – Selman Genç Jul 27 '14 at 20:53

1 Answers1

4

It seems that . is not the decimal separator in your locale - instead, it's probably ,, which means you'd have to enter 123,40.

However, you can use an invariant culture to parse a double where . is used as the decimal separator.

using System.Globalization;

Double.Parse("4.0", CultureInfo.InvariantCulture);

Also, it's highly recommended that you use Double.TryParse instead of Parse or Convert to validate user input and avoid exceptions.

dcastro
  • 66,540
  • 21
  • 145
  • 155