Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Number.toLocaleString() with 4 digits after separator

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

like image 255
Artem Avatar asked Jul 15 '14 12:07

Artem


People also ask

What does the toLocaleString () method do in JS?

The toLocaleString() method returns a Date object as a string, using locale settings. The default language depends on the locale setup on your computer.

How do I limit decimal places in JavaScript?

The toFixed() method rounds the string to a specified number of decimals.

Does toLocaleString round?

The toLocaleString() method will round the resulting value if necessary. The toLocaleString() method does not change the value of the original number.


1 Answers

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

number.toLocaleString(undefined, { minimumFractionDigits: 4 }) 
like image 129
Radagast Avatar answered Oct 20 '22 05:10

Radagast