Search code examples
javascripthtmlcsshandlebars.js

Is there a way to restrict number of decimal places shown for a number in output in Handlebars?


I am trying to display how much more a customer needs to spend in order to get free shipping on this website. I have the code working, but the output is not displayed as I expected. The price for the product is 29.99 but the result of the below handlebars code is returning the value 5.010000000000002. Is there a way in handlebars (or html) to display only the 5.01 and not the rest?

{{subtract 35 price.without_tax.value}}

Solution

  • I don't have any experience with Handlebars, but I can tell you that in plain JavaScript, you would handle this with Number's toFixed() method.

    var output = 35 - 29.99;
    
    console.log(output); // 5.010000000000002
    console.log(output.toFixed(2)); // 5.01