I need to format numbers in a given way for my website.
Here is my problem :
I have 3 different cases :
1) First case : When my number is negative, I have to put parenthesis around it, and display its absolute value. The thing is, I have to keep 1 digit after the comma, even for "round" number like (218, 9744, etc...)
2) Second case : When my number equals to zero, I have to return 0.0 [this one is OK]
3) Third case : When my number is positive, I have to return it with one digit after the comma.
To make it easy, I have to use toLocaleString()
function to format it in en-EN
.
The problem I'm facing seems to be the use of parseFloat()
which removes trailing zeros. Can someone help me ?
Here are a few examples of what I need :
218 -> 218.0
21800 -> 21,800.0
-21800 -> (21,800.0)
And my code :
if (number < 0) {
return ('(' + Math.abs(floatNumber.toFixed(1)).toLocaleString("en-EN") + ')');
}
else if (number == 0.0) {
return (floatNumber.toFixed(1))
}
else {
return (parseFloat(floatNumber.toFixed(1)).toLocaleString("en-EN"));
}