0

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>
Ataullah
  • 35
  • 1
  • 4

2 Answers2

1

Use Math.round() method: For round off 2 decimal digits:

var a = 12.24;
var b = 13.17;
var c = a * b;
c = Math.round(c * 100) / 100;
console.log(c);

If you want to truncate instead of round off use toFixed() method:

var a = 12.24;
var b = 13.17;
var c = a * b;
c = c.toFixed(2);
console.log(c);

This will truncate into two digits.

Marivishnu
  • 80
  • 1
  • 7
0

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);