Decimal in field number (html).

Asked

Viewed 10,784 times

1

How to put decimal (1.0 and 1.10) in step="0.010" of a type=number field? I tried to do but always zeros stay out, it is not possible to make a height 1.60 for example, the result is always 1,6.

  • That’s because the zeroes on the right are worthless. If you need this, you’ll have to wear a mask instead of a field like number.

1 answer

2


With this simple Javascript you solve it (the listeners are just for example, you do not need to use them. The code within them demonstrate how to add a 0 to the right of the decimal places):

document.getElementById("input").addEventListener("change", function(){
   this.value = parseFloat(this.value).toFixed(2);
});

document.getElementById("input").addEventListener("change", function(){
   this.value = parseFloat(this.value).toFixed(2);
});
<input id="input" type="number" step="0.010" />

Or with jQuery:

$("input").on("change",function(){
   $(this).val(parseFloat($(this).val()).toFixed(2));
});

$("input").on("change",function(){
   $(this).val(parseFloat($(this).val()).toFixed(2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input" type="number" step="0.010" />

  • good evening, the problem continues, does not catch 1,60. appears 1,6

  • php post. why, it doesn’t work like this??

  • how to know if the person put a 1.6 or 1.60 kkkkkk

  • if the person is height 1.6 the system will save 1.60? I guess it does not roll right

  • solved, but can be done in min="1.00"(html) and setting step="0.010"(html). then make a number format counting two digits for 1.60(1.6) and 3 three digits for 1.06. thanks for the help friend.

  • I’ll post here...

  • correcting (me="1"). <label for="user_lic"></label><input id="user_lic" type="number" min="1" step="0.010" />

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.