I have 3 inputs as two numbers are multiplied and result is written on the third input. However, I need to eliminate the decimals when it is completely unnecessary.
<input type="text" name="input1" id="input1" value="" size="4" class="inputt" />
<br>
<input type="text" name="input2" id="input2" value="" size="4" class="inputt" />
<br>
<input type="text" readonly="readonly" name="input3" id="input3" value="" size="4" class="inputt" />
For example; 150.00 should be indicated as 150, but 150.50 should be remain as it is.
And also, when the result is written after multiplication, I have to click on the readonly input to make the result well rounded. I need the final indication of the result number should happen without the need of any click. To understand better, write 12.5 to the first one, 12.1123 to the second and see the result.
$(document).ready(function () {
$("#input1, #input2").change(function() {
$("#input3").val($("#input1").val() * $("#input2").val());
var num =$("#input3").val();
num = num.toFixed(2);
});
});
$( function() {
$('#input1').keyup(function(){
if($(this).val().indexOf('.')!=-1){
if($(this).val().split(".")[1].length > 2){
if( isNaN( parseFloat( this.value ) ) ) return;
this.value = parseFloat(this.value).toFixed(2);
}
}
return this;
});
});
$( function() {
$('#input2').keyup(function(){
if($(this).val().indexOf('.')!=-1){
if($(this).val().split(".")[1].length > 5){
if( isNaN( parseFloat( this.value ) ) ) return;
this.value = parseFloat(this.value).toFixed(2);
}
}
return this;
});
});
$('input#input3').blur(function(){
var num = parseFloat($(this).val());
var cleanNum = num.toFixed(2);
$(this).val(cleanNum);
});
Here are the codes http://jsfiddle.net/mLfkd/2/