0

I am trying to set a default value for my number input tag. Here is the code:

<input id="discount-input" type="number" step="any" [email protected] />

Model.OrderDiscount is a decimal value, that holds 2,00. When I run the code, the input field is just blank. I tried another variable from Model, and that worked fine. That was an integer though, so I also hard coded value=2.00. I also tried all different combinations with step="0.01", step="0.1", step="1" and step="any" with the same result.

I know that you think that OrderDiscount just have to be empty, but no! I checked with the debugger 10 times. What is going on?

Edit: I tried another field on Model that also contained a decimal value. Neither this was displayed. It seems as I cannot use a variable to set a default decimal value.

1 Answer 1

5

Try adding double quotes around the value and also make sure that your decimal separator is . instead of ,:

<input id="discount-input" type="number" step="any" value="@Model.OrderDiscount.ToString(CultureInfo.InvariantCulture)" />
Sign up to request clarification or add additional context in comments.

4 Comments

I did, I got the same result.
Make sure that you have the correct decimal separator in the markup: 0.2 and not 0,2. I have updated my answer to illustrate that.
You are right, the code generates a , instead of a .. But how is that even possible? OrderDiscount holds a decimal variable type.
It's very simple. It just calls the ToString() method on it. And this method uses the decimal separator configured on your operating system. So depending on what culture you have selected to be used, it will use this format. By using .ToString(CultureInfo.InvariantCulture) you explicitly indicate that you want to use the invariant culture in which the decimal separator is .. This will guarantee that your code will work no matter the settings of the underlying OS on which you have installed your App.

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.