Adding ".00" When Returning Dollar Value Totals Via Math.js

131 Views Asked by At

In my MongoDB/Node backend I am doing some totaling of dollar amounts and returning that data to the front end. Note this is for displaying totals only, we're not altering the values themselves. Because we want two decimal places for the totaled dollar values, I am doing this:

let roundedTotalOpenBalance = math.round(totalOpenBalance, 2);

That will give me something like -- 322.45 -- which is what I want.

However, if the value total is 322, I'd like to also pass that to the front end as 322.00. How can I do this with math js? Or do I need to handle the transformation myself?

2

There are 2 best solutions below

1
A l w a y s S u n n y On BEST ANSWER

Discard Math.round() and Try with toFixed()

let num1 = 322.45;
let roundedTotalOpenBalance1 = num1.toFixed(2);
console.log(roundedTotalOpenBalance1);

let num2 = 322;
let roundedTotalOpenBalance2 = num2.toFixed(2);
console.log(roundedTotalOpenBalance2);

0
Maciej Wojsław On