Skip to content Skip to sidebar Skip to footer

Math.round Not Working

I'm trying to use Math.round for the total to only show 2 decimals but it doesn't do that. What am I doing wrong? $(document).ready(function() { var totalPrice = 0; $('.fo

Solution 1:

You Math.round is let alone without assignment variable such as:

totalPrice = Math.round(totalPrice * 100) / 100;
//  ^^ you're missing an assignment to store your operation result

Here's another suggestion for the two decimals:

totalPrice = Number( (totalPrice * 100) / 100).toFixed(2) );
$(".totalPrice").text("Total Price: $" + totalPrice);

Solution 2:

If you work with currency it is better to use fixed point numbers for precision. There is no fixed-point number type in javascript, so consider using external libraries. E.g. Big.js or bignumber.js

Post a Comment for "Math.round Not Working"