0

I searched in "StackOverflow" but I couldn't find the answer. My question: I want to write a project with asp.net related to the numbers. It need work with "Decimal" data (EX: 19.5), but in some countries like canada instead of . they use , that means if I write this code, I'll face with the error:

'in Canada
Dim A as Double =  Convert.ToDouble("19.5")  'Error
Dim A as Double =  Convert.ToDouble("19,5")  'OK

'in USA
Dim A as Double =  Convert.ToDouble("19.5")  'OK
Dim A as Double =  Convert.ToDouble("19,5")  'Error

I want my code work for all countries like USA which use 19.5 instead of 19,5.

  • How can I check the Regional and language setting and accept 19.5 in USA and 19,5 in Canada.
  • Or, How can I accept both in my program, without manipulating the string or using Validator or try-catch.

I appreciate you in advanced.

Veve
  • 6,643
  • 5
  • 39
  • 58
Malus Jan
  • 1,860
  • 2
  • 22
  • 26
  • 1
    Actually there are a *lot* of duplicate questions - just pass `CultureInfo.InvariantCulture` as the second parameter. Almost all parsing/formatting methods have an overload with an IFormatProvider parameter that can accept a specific culture. Also check the [Parse](https://msdn.microsoft.com/en-us/library/fd84bdyt(v=vs.110).aspx), [TryParse](https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx) methods that provide better control on how a string is parsed – Panagiotis Kanavos Feb 22 '16 at 15:22
  • 1
    Most .NET types have a Parse, TryParse method which provide much better parsing control. `Convert.ToDouble` for example, doesn't allow you to specify the number styles used while `Parse` allows you to specify scientific notation, hex numbers etc – Panagiotis Kanavos Feb 22 '16 at 15:30
  • I found myself the exact answer [here](http://stackoverflow.com/questions/1354924/how-do-i-parse-a-string-with-a-decimal-point-to-a-double) – Malus Jan Mar 18 '16 at 22:34

0 Answers0