admin管理员组文章数量:1327692
I've been racking my head as to trying to understand why when using Number.prototype.toLocaleString
, it is multiplying my number by 100.
var num = 5;
alert(num + ' vs ' + num.toLocaleString('en-GB', { style: 'percent' }));
I've been racking my head as to trying to understand why when using Number.prototype.toLocaleString
, it is multiplying my number by 100.
var num = 5;
alert(num + ' vs ' + num.toLocaleString('en-GB', { style: 'percent' }));
Reading through all the reference guides, I have not seen anything that suggests it should be.
Have I pletely missed the point?
Share Improve this question edited Sep 6, 2017 at 13:59 msanford 12.2k13 gold badges71 silver badges98 bronze badges asked Sep 6, 2017 at 13:56 GavinGavin 6,3945 gold badges32 silver badges39 bronze badges3 Answers
Reset to default 4From the spec:
If the value of numberFormat.[[style]] is "percent", let x be 100 × x.
it expects a number in the range of [0, 1] which then would be formated to something between 0% and 100%
It's percent
not toLocaleString
that is causing this, and the reason is that the percentage is measured by numbers from 0
to 1
as 0%
to 100%
, so if you want 5%
, use :
var num = .05;
alert(num + ' vs ' + num.toLocaleString('en-GB', { style: 'percent' }));
More @ Number.prototype.toLocaleString()
As many other systems do, toLocaleString
's percent style interprets 1 as 100 %.
This is a mon way of expressing percentages (such as in probability theory where a 100 % probability is 1
).
版权声明:本文标题:javascript - Formatting percentages using toLocaleString causes value to be multiplied by 100 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742219656a2435213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论