Search code examples
javascripthtmlroundingcalculatormultiplication

After multiplication, i want output to be in 2 decimals? how to do that?


I want to calculate multiplication from gien input. However, I want output to be not more than 2 decimals.

For e.g. Multiplication of 12.24 and 13.17, is 161.2008. I want output truncated to 2 decimals, i.e. output should be 161.20.

I am not a professional one, and working just to make this project work. so if you guys can edit my code and teach me, that would also be appreciated.

I have pasted part of the code of my project here:

function dbkcalc()
        {
  
        var decde = (document.getElementById('DECDAPR').value * document.getElementById('DECDAPC').value);
        document.getElementById('DECDE').value= decde;
        }
<style>
    td
        {
        width: 50%;
        border: 1px solid ORANGE;
        }

      

</style>

<body>

<table align="center">


    <tr>
        <td>DECDAPR</td>
        <td><input id="DECDAPR"></td>
    </tr>

    <tr>
        <td>DECDAPC</td>
        <td><input id="DECDAPC"></td>
    </tr>

    <tr>
        <td colspan=2>
            <input id="CALDIF" type="button" class="button" value="CALCULATE" onClick="dbkcalc()">
        </td>
    </tr>

    <tr>
        
        <td><U>DECDE</U></td>
        <td class="declared"><U><output id="DECDE" type="number"></U></td>
    </tr>

</table>

</body>


Solution

  • This has already been asked here. You should really try to google these simple things before posting. But anyway just change that 1 javascript code line to document.getElementById('DECDE').value = decde.toFixed(2);