Tofixed() Simple Use/explanation In Text Field
I've avoided asking this question here as I know many have in the past. I've spent some time during the last few days trying to find a solution/figure out how the toFixed() method
Solution 1:
A few things:
- Use the
change
event instead ofkeyup
. If you usekeyup
, the text wil change every time the user tries to type something, which is an annoying user experience. - Consider using an
input
of typenumber
with a step of0.1
.
With those in mind, I'd do something like this:
$('.entry').change(function(){
// parse the typed value as a floating point numbervar num = parseFloat(this.value);
// ensure there are two decimal placesthis.value = num.toFixed(2);
});
Note that if the user types something with more than two decimal places, the value will be rounded.
Example:http://jsfiddle.net/jndt1e02/
Post a Comment for "Tofixed() Simple Use/explanation In Text Field"