最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Decimal and Thousand separator of toLocaleString() method? - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

Somthing 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"));

发布评论

评论列表(0)

  1. 暂无评论