Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript math, round to two decimal places in Jquery template

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.

like image 919
danil Avatar asked Jul 22 '14 06:07

danil


People also ask

How do you round to 2 decimal places in jQuery?

To round off decimal values using jQuery, we can use the built-in JavaScript methods toFixed() or toPrecision(). The toFixed() method converts a number into a string, keeping a specified number of decimals. The toPrecision() method formats a number to a specified length.

How do I round to 2 decimal places in JavaScript?

Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places.

How do you round to 2 decimal places in Math?

Or, if you want to round 0.2345 to two decimal places, you need to round 23.45 (0.2345*100), then divide the result (23) by 100 to get 0.23.

How do I round a number in jQuery?

use javascript's Math. ceil() method to round numbers up. Save this answer.


1 Answers

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

like image 128
Balachandran Avatar answered Sep 22 '22 17:09

Balachandran