I have a field:
<input type='number' />
I'd like to punch in 0.50
without it “correcting it” to 0.5
, so it would display 0.50
.
I have a field:
<input type='number' />
I'd like to punch in 0.50
without it “correcting it” to 0.5
, so it would display 0.50
.
I attached an on('change') event to the input you want to have trailing 0's
It just takes the value, casts it to a float, renders it to a string to the number of decimal places, and puts it back in as the value.
I've had a little play around with this and looked at the spec. It says that it must be a valid floating point number. There's one sentence in the definition of a valid floating point number it gives which caught my attention:
This means that the format will always be consistent with assessing what the number is, then using JavaScript's toString on that number. So no trailing 0s then.
So, you're going to have to resort to JavaScript. This isn't straightforward because
document.getElementById('numInput').value = '0.50';
still gets corrected to0.5
, so the validation isn't triggered atonchange
where the default action can be prevented, it's triggered internally.This is the best solution I could come up with... it's a bit of a hack, and will need a bit of tweaking for robustness, but hopefully it'll do what you want:
So if the user wants to enter the number by typing, it switches the input type to text, but when they click it, it converts it back to a number.
If you always want the trailing 0s no matter what the user types, then you could do it something like this:
Edit: I think the second solution is more inline with what the user might expect, but it means that if the user types
0.5
it will be coerced to0.50
, so it depends if that's what you want.