1

I did a script to sum 2 text field values and automatically get the value in a other text box result, the problem is that is not doing the sum using decimals.

I want to round each text box to 2 decimals if it has more automatically convert to 2 decimals and get the result to 2 decimals.

Here is the demo:

<script type="text/javascript">
  jQuery("#ige").live("change", function(){
    ige_value = parseInt(jQuery(this).val());
    sba_value = parseInt(jQuery("#sba").val());           
    total = ige_value + sba_value  
 jQuery("#result").val(total.toFixed(2));   
 });
</script> 

FIRST VALUE<input id="ige" name="ige" type="text" > <br/> 
SECOND VALUE <input id="sba" name="sba" type="text" ><br/> 
RESULT: <input id="result" name="result" type="text" >

enter image description here

Pleas somebody can help me with this?

3 Answers 3

4

Try using parseFloat. Here you are using integer which doesn't support decimals.

Use toFixed(2); for the formatting

More here: Format number to always show 2 decimal places

3
2

The problem is that you are using parseInt:

 ige_value = parseInt(jQuery(this).val());

Integers have no decimal values try replacing it with:

 ige_value = parseFloat(jQuery(this).val());

You can use .toFixed(2) to always show 2 decimals on the result.

3
  • Thanks but hwo about auto format to 2 decimals whatever i type Oct 27, 2014 at 4:47
  • Do you know how to do to do the calculation in whatever input changed? because if I have 20 inputs I need to do the same 20 times, can you help me or have the information? Oct 27, 2014 at 14:56
  • It's probably better to start a new question for that, so that you can provide some more background information
    – JanR
    Oct 27, 2014 at 23:01
1

Here is another answer that also seems to repair some possible logic errors in the selection of the "ige" value being changed. First, here is the code and then will come a new jsFiddle:

The HTML

FIRST VALUE
<input id="ige" name="ige" type="text">
<br/>SECOND VALUE
<input id="sba" name="sba" type="text">
<br/>RESULT:
<input id="result" name="result" type="text">

and now the JavaScript

jQuery("#ige, #sba").live("change", function () {
   var ige_value = Math.round(parseFloat(jQuery("#ige").val()) * 100.0) / 100.0;
   jQuery("#ige").val(ige_value);
   var sba_value = Math.round(parseFloat(jQuery("#sba").val()) * 100.0) / 100.0;
   jQuery("#sba").val(sba_value);
   var total = ige_value + sba_value;
   jQuery("#result").val(total.toFixed(2));
});

What the JavaScript does is look for a change in either "#ige" or "#sba" and do its calculations appropriately. The values of these two input text fields are also changed per your rules.

Here is a new jsFiddle for testing:

jsFiddle

1
  • Thanks for your other way of doing this. +1 Oct 27, 2014 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.