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