Skip to content Skip to sidebar Skip to footer

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:

  1. Use the change event instead of keyup. If you use keyup, the text wil change every time the user tries to type something, which is an annoying user experience.
  2. Consider using an input of type number with a step of 0.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"