JavaScript(JS) number method - tolocalestring
The toLocaleString()
method is a built-in method of the Number
object in JavaScript. It returns a string representation of the number formatted according to the locale settings of the user's system.
Here's an example usage:
refer to:editfigia.comconst num = 123456.789; console.log(num.toLocaleString()); // "123,456.789" in the US locale
In the above example, the toLocaleString()
method is called on the num
variable, which is assigned the value 123456.789
. The method returns a string representation of the number formatted according to the user's locale settings. In the US locale, the decimal separator is a period and the thousands separator is a comma, so the output is "123,456.789"
.
The toLocaleString()
method can also take optional arguments to customize the formatting of the output. For example:
const num = 123456.789; console.log(num.toLocaleString("de-DE", { style: "currency", currency: "EUR" })); // "123.456,79 €" in the German locale
In this example, the method is called with two arguments. The first argument is a string specifying the desired locale ("de-DE" for German), and the second argument is an options object specifying that the output should be formatted as currency in euros. The output in the German locale is "123.456,79 €"
, where the decimal separator is a comma and the thousands separator is a period, and the currency symbol is the euro symbol.