cheslijones
cheslijones

Reputation: 9194

.toLocaleString() not showing commas on numbers with decimals

.toLocaleString() works great on my integer data types; however, it isn't working for numbers with decimals.

I have some large currency numbers that I divide by 1000 to present in thousands and .toFixed(1) decimals spot. So 1315321.56 becomes 1315.3. What I have is this:

<td>${(fieldObj/1000).toFixed(1)}</td>

When I tried to add .toLocaleString() with the intent of it rendering 1,315.3 nothing happens and it just displays it as normal. How can I get around this?

To be clear, this is what I have with it added:

<td>${(fieldObj/1000).toFixed(1).toLocaleString()}</td>

Upvotes: 7

Views: 11194

Answers (1)

Kevin Hakanson
Kevin Hakanson

Reputation: 42200

toFixed returns a "string representing the given number using fixed-point notation" and not a number, so you can't call toLocaleString on the result

You can use the Intl.NumberFormat options to round to 1 digit.

(1315321.56/1000).toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1})
"1,315.3"

Upvotes: 13

Related Questions