JavaScript math, round to two decimal places in Jquery template

danil picture danil · Jul 22, 2014 · Viewed 33.3k times · Source

I have some code

{{if commission}}              
    <td>${profit - commission}</td>   
{{else}}
    <td>${profit}</td>
{{/if}}

profit = 5;

commission = 2.145

result = 2.855999999999

I need 2.856

Please help me

I try to use (${profit - commission}).toFixed(2) - but it does not work.

Answer

Balachandran picture Balachandran · Jul 22, 2014

Use simply toFixed(3) .it will select the 3 digits value after the dot value

var s=2.855999999999;
alert(s.toFixed(3))

OP: 2.856

DEMO