Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toLocaleString() don't show zero

Tags:

javascript

Hello i have a question about:

Number.prototype.toLocaleString()

Trouble is that when we use "toLocaleString()" method it will not show latest maximumFractionDigits value if there was "0".

var i = 50456.40345345;
i.toLocaleString('en-US', {maximumFractionDigits: 2})
// It will return "50,456.4".
// I want to see "50,456.40".

How can i do that?

like image 226
Valee Avatar asked Jan 17 '26 02:01

Valee


1 Answers

Use minimumFractionDigits:

var i = 50456.40345345;
i = i.toLocaleString('en-US', {maximumFractionDigits: 2, minimumFractionDigits: 2});
document.write(i);

Although you are specifying the maximumFractionDigits, the numbers don't necessarily have 2 fraction digits, so that's why you need to specify minimumFractionDigits.

like image 121
user1823 Avatar answered Jan 19 '26 15:01

user1823



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!