最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - .toLocaleString() not showing commas on numbers with decimals - Stack Overflow

programmeradmin2浏览0评论

.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 bees 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>

.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 bees 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>
Share Improve this question asked Feb 6, 2018 at 23:06 cheslijonescheslijones 9,19424 gold badges99 silver badges208 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

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"
发布评论

评论列表(0)

  1. 暂无评论