[ACCEPTED]-Using jQuery to display a value to 2 decimal places in an input box while retaining more precision-numbers

Accepted answer
Score: 15
num = 12.434234234;
result = num.toFixed(2); // returns 12.43

0

Score: 6

You can store the fine-grained values in 2 hidden fields, and trim the ones displayed 1 for the user.

Score: 4

You could store the value of the input as 3 data of the input (using jQuery), format it 2 onload and then replace the value during 1 submit like so:

http://jsfiddle.net/magicaj/8cdRs/1/

HTML:

<form id="form">
    <input type="text" class="formatted-number-input" value="12.434234234" />
</form>

JS:

$(".formatted-number-input").each(function() {
   var value = $(this).val(); 
   $(this).data("originalValue", value);
   var roundedValue = value.toFixed(2);
   $(this).val(roundedValue);
});

$("#form").submit(function() {    
    var formattedInput = $(".formatted-number-input");
    formattedInput.each(function() {
        var originalValue = $(this).data("originalValue");
        $(this).val(originalValue);
    });
});
Score: 1

There are multiple ways to solve this problem: 1. Create 4 hidden field, store 12.434234234 in it and 3 display formatted value in textfield. 2. Store 2 original 12.434234234 using jquery.data() and 1 display formatted value in textfield.

More Related questions