Reputation: 2402
I want to remove the .00 decimals. I´m using Number(...) for for values like 100.50 it returns the incorrect decimals.
Number(100.00) // 100 Perfect!
Number(100.50) // Actual: 100.5 - Expected: 100.50
Upvotes: 0
Views: 41
Reputation: 10627
I'd (well, I wouldn't but you might) do like:
function numberFix(number){
return number % 1 === 0 ? number.toString() : number.toFixed(2);
}
console.log(numberFix(100.00));
console.log(numberFix(100.5));
Upvotes: 2
Reputation: 23
You have to check manually and then print differently based on the result.
try this
var num1 = 100.50;
var num2 = 100.00;
function numToString(num)
{
if (num - Math.floor(num) > 0)
{
console.log(num.toFixed(2))
} else {
console.log(num);
}
}
numToString(num1); // 100.50
numToString(num1); // 100
Upvotes: 0
Reputation: 23
Try using toFixed(n)
for example
Number(100.00).toFixed(2) // 100.00
Number(100.00).toFixed(0) // 100
Number(100.50).toFixed(2) // 100.50
Upvotes: 0