2

How can i did an script to multiply 2 inputs and sum results my question is: "How can i do to put 2 decimals on each input?

 soles * cost      = subtotal
 subtotal + dolares = total  

This is my view html

   <label>Sum Soles :</label>
   <input id="soles" value="2233.3234333" /> 

   <label>Sum Dolars :</label>
   <input id="dolars" value="3244.3566" />

   <br/><br/>
   <label>Cost of Dolar</label>
   <input type="text" id="cost" maxlength="5" onchange="doMath();" />

   <label>Total Soles to Dolars</label>
   <input id="subtotal" readonly="readonly" />

   <br/><br/>  
   <label>SUM TOTAL</label>
   <input id="total" readonly="readonly" />  

This is my script

<script type="text/javascript">
function doMath()
{
    // Capture the entered values of two input boxes
    var soles = document.getElementById('soles').value;
    var cost = document.getElementById('cost').value;
    var dolares=document.getElementById('dolars').value;

    // Add them together and display
    var subtotal = parseFloat(soles) * parseFloat(cost);
    document.getElementById('subtotal').value = subtotal;

    var total = parseFloat(subtotal) + parseFloat(dolars);
    document.getElementById('total').value = total;
}
</script>

I expect as result

Sum Soles : 2233.32
Sum Dolars 3244.36
Cost  : 1.2
Total Soles to dolars = 2679,98
Total :5924,34

Please somebody can help me with this?

I will appreciate help

Carlos Morales
  • 1,137
  • 3
  • 15
  • 38
  • possible duplicate of [JavaScript: formatting number with exactly two decimals](http://stackoverflow.com/questions/1726630/javascript-formatting-number-with-exactly-two-decimals) – Ryan Oct 31 '13 at 16:33
  • It isn't a duplicate, i created it right now – Carlos Morales Oct 31 '13 at 16:52
  • Carlios - I edited your question so it's correct English. You changed it back. I'm not going to get into an edit war, but it's BEST NOT TO WRITE YOUR QUESTIONS IN ALL CAPITALS, and your question now is not correct English. – Joe Oct 31 '13 at 22:14
  • Sorry i did a mistake, by the way thanks – Carlos Morales Oct 31 '13 at 22:18
  • Well i tried to correct some words , sorry i'm new on this and try to learn. – Carlos Morales Oct 31 '13 at 22:22

2 Answers2

2

You can use the number.toFixed(2) prototype.

Edit: This will return 1.20 for Costo and not 1.2. If you wish to round to 2 decimals, you can use the Math.round method that tak3r provided.

Here's a prototype that does the same thing:

Number.prototype.roundToDecimals = function(decimals) {
    decimals = decimals || 0;
    var pow = Math.pow(10, decimals);
    return Math.round(this * pow) / pow;
};

> (1.234567).roundToDecimals(2); 
1.23

> (1.2).roundToDecimals(2); 
1.2
Community
  • 1
  • 1
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
2

you can use .toFixed(2) or mathematically Math.floor(number * 100) / 100 -> they are interchangeable but .toFixed() returns a string so keep that in mind

function doMath()
{
    // Capture the entered values of two input boxes
    var soles = document.getElementById('soles').value;
    var costo = document.getElementById('costo').value;
    var dolares=document.getElementById('dolares').value;

    // Add them together and display
    var subtotal = Math.floor(parseFloat(soles) * 100) / 100 * Math.floor(parseFloat(costo) * 100) / 100;
    document.getElementById('subtotal').value = subtotal.toFixed(2);

    var total = parseFloat(subtotal) + parseFloat(dolares);
    document.getElementById('total').value = total;
}
Stefan
  • 3,962
  • 4
  • 34
  • 39
  • I tried your code and understoo all clear. Thanks 1 point :) and thanks for helping – Carlos Morales Oct 31 '13 at 16:51
  • i have another question how can i do it on soles and dolares to use 2 decimals? – Carlos Morales Oct 31 '13 at 16:56
  • you can just the same code but apply it to the values. Or are you referring to having the input fields themselves have at most 2 decimals when the user is typing? – Stefan Oct 31 '13 at 17:04
  • Well can you check my example what i expect? . There i wrote some values for example **Suma Soles : 2233.32 **Suma dolares 3244.36 **Costo : 1.2 **Total Soles TO dolares = 2679,98 **Total :5924,34. I always want round 2 values on all my inputs – Carlos Morales Oct 31 '13 at 17:10
  • for that check @Jef Noel's comment on http://stackoverflow.com/questions/18273209/force-decimals-in-html-inputs-with-inputmask-html-jqueryl-or-js – Stefan Oct 31 '13 at 17:27
  • I checked and that example is about adding 000 after the symbol ' . ' .What i want is this and it will show you 2333.36 – Carlos Morales Oct 31 '13 at 18:33
  • Don't worry i understood it :) thanks for everything. – Carlos Morales Oct 31 '13 at 18:56