admin管理员组文章数量:1386706
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"));
本文标签: javascriptHow to get Decimal and Thousand separator of toLocaleString() methodStack Overflow
版权声明:本文标题:javascript - How to get Decimal and Thousand separator of toLocaleString() method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744567644a2613149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论