What's the best way to format a number?
- If decimal is less than
2
digits, add zero to make it2
decimals - If more than
4
, truncate to4
decimals If
3
decimals, keep it to3
decimals14 => 14.00 14.1 => 14.10 14.123 => 14.123 14.12347 => 14.1234
I don't want rounding to happen
if (number !== 0) {
nParts = number.toString().split('.');
if (nParts[1]) {
if (nParts[1].length > 4) {
nParts = 4;
} else if (nParts[1].length < 3) {
nParts = 2;
} else {
nParts = 3;
}
} else {
nParts = 2;
}
}
number = number.toFixed(nParts);
Help me improve on this.
987,24
\$\endgroup\$