JavaScript Number.toLocaleString() with 4 digits after separator

Artem picture Artem · Jul 15, 2014 · Viewed 43.1k times · Source

Is there a way to get number.toLocaleString() with 4 digits after the comma?

Example:

var number = 49.9712;
document.getElementById('id').innerText = number.toLocaleString();

Result: 49,9712

But now it always returns number with 2 digits after comma: 49,97

Answer

Radagast picture Radagast · Apr 21, 2015

You may use second argument to provide some options. In your case, with default locale:

number.toLocaleString(undefined, { minimumFractionDigits: 4 })