MVC3: sending string - parsing fails in IE and Chrome
I have a problem with the submitted form of information. Decimal parsing fails when I try to parse a string returned via Interner Explorer or Chrome, but not Firefox or Safari. Strings look exactly the same in Visual Studio. I did this debug bit:
var asd3 = collection["formValue"]; // Get it from the FormCollection
var asd4 = asd3.Replace(",", "."); // Change the punctuation
var asd5 = Decimal.Parse(asd4); // Make the string into a decimal
var asd6 = Math.Round(asd5, 1); // Round it
Unable to execute asd5
when trying to parse decimal from asd4
with error:Input string was not in a correct format.
Here is a picture of the strings. At the top is Firefox and below Internet Explorer.
What could be the problem here?
source to share
What could be the problem here?
Culture.
Check the value in your debugger Thread.CurrentThread.CurrentCulture
and you will see differences between browsers.
If your browser has a different culture configured, that culture will be used by ASP.NET when parsing values, especially if you haven't specified an explicit culture in your web.config:
<globalization culture="en-US" uiCulture="en-US" />
If this parameter is set to a value auto
then browser culture will be used.
Another possibility is to force invariant culture on parsing to ensure that .
(dot) is the decimal separator.
var asd5 = Decimal.Parse(asd4, CultureInfo.InvariantCulture);
source to share