5

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.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
danil
  • 117
  • 1
  • 2
  • 6

6 Answers6

13

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

Balachandran
  • 9,567
  • 1
  • 16
  • 26
2
var result = 2.855999999999;
result =   result.toFixed(2); //returns string fixed to 2 decimal places
result =   parseFloat(result);//returns double 2 decimal places
alert(result);
Dedan
  • 125
  • 9
2

Mentioned as using in jQuery template

parseFloat should be inside ${}

${ parseFloat(value).toFixed(2)}

{{if commission}}              
    <td>${parseFloat(profit - commission).toFixed(2)}</td>   
{{else}}
    <td>${profit}</td>
{{/if}}
1

You can use:

var result = 2.855999999999;
result = Math.round(result * 1000) / 1000;
console.log(result ); //  ----> 2.856

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Working fiddle here

You can use Math.round()

var num = 2.855999999999
num = Math.round(num * 1000) / 1000
alert(num);

Use 100 for .xx etc :D

hahaha
  • 1,001
  • 1
  • 16
  • 32
0

Try this:

parseFloat(${profit - commission}).toFixed(3);