How to get Decimal and Thousand separator of toLocaleString() for the locale selected?
Example:
var number = 123456.789;
// German uses ma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
123.456,789
// English uses period as decimal separator and ma for thousands
console.log(number.toLocaleString('en-GB'));
123,456.789
Is there any way to get locale separators?
How to get Decimal and Thousand separator of toLocaleString() for the locale selected?
Example:
var number = 123456.789;
// German uses ma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
123.456,789
// English uses period as decimal separator and ma for thousands
console.log(number.toLocaleString('en-GB'));
123,456.789
Is there any way to get locale separators?
Share Improve this question asked Apr 1, 2019 at 13:59 Diogo PeresDiogo Peres 1,3722 gold badges11 silver badges22 bronze badges 1- @Amy this question is about how to get at the characters used in the current locale, and that question doesn't address that. – Pointy Commented Apr 1, 2019 at 14:06
2 Answers
Reset to default 3Somthing like this should work (not tested):
let thousandsSeparator = Number(1000).toLocaleString().charAt(1)
let decimalSeparator = Number(1.1).toLocaleString().charAt(1)
you can get decimal separator and group separator from value. I can write below two function:
function getDecimalSeparator(locale) {
const numberWithDecimalSeparator = 1.1;
return Intl.NumberFormat(locale)
.formatToParts(numberWithDecimalSeparator)
.find(part => part.type === 'decimal')
.value;
}
function getNumberGroupSeparator(locale) {
const numberWithDecimalSeparator = 1000.1;
return Intl.NumberFormat(locale)
.formatToParts(numberWithDecimalSeparator)
.find(part => part.type === 'group')
.value;
}
console.log(getDecimalSeparator("en"));
console.log(getDecimalSeparator("fr"));
console.log(getNumberGroupSeparator("en"));
console.log(getNumberGroupSeparator("fr"));