I wonder what would be the shortest and safest way to achieve the following:
Suppose I have a float var with the value 1.200123
. In our visualization, I want to show this value with two decimal places, so I use myFloat.toFixed(2);
at the moment, which gives me 1.20
.
That's fine. But what if the resulting output only has zeros at the decimal places? For example the value 1.000043
which becomes 1.00
.
For the case above: how can I omit the decimal point and the zeros and just get 1
as output, preferrably without parsing and manipulating the string that comes out of .toFixed(2)
?
...preferrably without parsing and manipulating the string that comes out of .toFixed(2)?
Well, you could do it numerically, though I'd worry about getting a perfect match between numeric logic and toFixed
's logic, given the definition of toFixed
includes phrases like:
Let n be an integer for which the exact mathematical value of n ÷ 10f - x is as close to zero as possible. If there are two such n, pick the larger n.
So I'd probably just update the string, it's not a lot of work:
function formatNumber(num) {
return num.toFixed(2).replace(/\.00$/, "");
}
function test(num) {
console.log(num, "=>", formatNumber(num));
}
test(1.01);
test(1.43);
test(23.7);
test(1.200123);
test(1.000043);
test(1.007);
test(1.0007);
But here's an approximate numeric approach, which at least matches the results for the test cases above:
function formatNumber(num) {
var check = Math.round(num * 100) / 100;
var rounded = Math.round(check);
if (rounded == check) {
return String(rounded);
}
return num.toFixed(2);
}
function formatNumberViaToFixed(num) {
return num.toFixed(2).replace(/\.00$/, "");
}
function test(num) {
var formatted = formatNumber(num);
var viaToFixed = formatNumberViaToFixed(num);
if (formatted == viaToFixed) {
console.log(num, "=>", formatted);
} else {
console.log(num, "=>", formatted, "ERROR: Should be", viaToFixed);
}
}
test(1.01);
test(1.43);
test(23.7);
test(1.200123);
test(1.000043);
test(1.007);
test(1.0007);
.as-console-wrapper {
max-height: 100% !important;
}