-1

I have this JS file. This snippet will display Delivery Charge but its in 4 decimal places. I want it to display only two decimal places. How to change the code?

if (val.order_id > 0) {
  html += '<div class="col text-right">';
  html += "<b class='amout'>" + val.order_total_amount + "</b>";
  html += "<b class='charge_thaitop " + val.payment_type_raw + "'>" + getTrans('Delivery Fee ', 'deliver_fee') + getTrans('RM', 'MYR') + val.delivery_charge + "</b>";
  html += '</div>';
}

The + val.delivery_charge+ will output the number but currently its in 4 decimal places. I want it in two decimal places only. Refer the output in the picture below:

output in red rectangle

Thanks

brk
  • 48,835
  • 10
  • 56
  • 78
  • use `toFixed(2)` – brk May 03 '20 at 08:58
  • 2
    Does this answer your question? [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) Please check if question already exists :-) – thex May 03 '20 at 09:04

2 Answers2

0

You can convert it to a string and count below the comma and check if ther are more or less than 2 chars.

Or method .toFixed(2)

kornholio
  • 33
  • 6
0

I have found the solutions.

Sorry im not familiar with this JS.

But i put prettyPrice(val.delivery_charge) and its worked.

Thanks for the suggestion