首页 > 解决方案 > Number Format on Array of Int Jquery

问题描述

I have an array look like these :

console.log(myArray);

myArray: [100000, 300000, 20000000, 3450000, 610000]

What I want to do is converting my array into like these:

myArray: [100.000, 300.000, 20.000.000, 3.450.000, 610.000]

So far, I've tried code:

for (let i = 0; i < myArray.length; i++) {
   const element = (myArray[i]/1000).toFixed(3);
}
console.log(element);

But it seems didn't work at all. Anyone can help me to solve these?

Thanks.

标签: javascriptjqueryarraysnumber-formatting

解决方案


您可以使用Intl.NumberFormat与所需输出匹配的语言环境(例如德语/德国):

const array = [100000, 300000, 20000000, 3450000, 610000];
const formatter = new Intl.NumberFormat('de-DE');
const formatted = array.map(formatter.format);

console.log(formatted);


推荐阅读