xxxxxxxxxx
// The way that is often shown is this:
let rounded = number.toFixed(2);
// Doing it this way results in "rounded" being of type String
// To fix this, use the parseFloat() method, either directly or in two steps:
/* 1 (Directly): */
let rounded = parseFloat(number.toFixed(2);
/* 2 (Indirectly): */
let stringRounded = number.toFixed(2);
let rounded = parseFloat(stringRounded);
xxxxxxxxxx
var subTotal="12.1345";// can also be int, float, string
var subTotalFormatted=parseFloat(subTotal).toFixed(2); //"12.13"
xxxxxxxxxx
function roundTo2(num) {
return Math.round( ( num + Number.EPSILON ) * 100 ) / 100;
}
xxxxxxxxxx
var num = 0,19784342446461994;
num = num.toFixed(5); // get total 5 number => 0,19784
num = num.toFixed(6); // get total 6 number => 0,197843
num = num.toFixed(7); // get total 7 number => 0,1978434