[ACCEPTED]-Using jQuery to display a value to 2 decimal places in an input box while retaining more precision-numbers
Accepted answer
num = 12.434234234;
result = num.toFixed(2); // returns 12.43
0
You can store the fine-grained values in 2 hidden fields, and trim the ones displayed 1 for the user.
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);
});
});
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.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.