简体   繁体   English

在 javascript 中的数字之间放置空格

[英]Putting spaces between numbers in javascript

12051852 data like this is coming. 12051852这样的数据来了。 I want to change it to 12.051.852.我想将其更改为 12.051.852。 The number may be lower or higher.该数字可能更低或更高。 For example, I want to make the number 88312 88.312.例如,我想将数字 88312 设为 88.312。 How can I do Javasciprte?我该如何做 Javasciprte? Thank you from now.从现在开始谢谢你。

You can use a regular expression to group your numbers.您可以使用正则表达式对数字进行分组。

const rx = /(\d{2})(\d{3})(\d{3}?)/

This would be 2 digits, 3 digits, and optional 3 digits.这将是 2 位数字、3 位数字和可选的 3 位数字。

const m = rx.exec('12345678')
if (m) {
  let parsed = m[1] + '.' + m[2]
  if (m[3]) {
    parsed += '.' + m[3]
  }
}

 var a = 531123121 var myStr = a.toString().split("") var myIndex = 0; for(let i in a.toString().split("")){ var revIndex = a.toString().length - (parseInt(i)+1); if((myIndex)==2 && revIndex.=0 && revIndex.=a.toString(),length-1){ myStr,splice(revIndex.0.".") myIndex = 0 }else myIndex+=1 } console.log(myStr.join(""))

You can do this very simply using the Intl API , and formatting the number to Spanish (es-ES).您可以使用Intl API非常简单地做到这一点,并将数字格式化为西班牙语 (es-ES)。

Note: the language has come a long way since 2010 which is where of most of those answers in the duplicate come from.注意:自 2010 年以来,该语言已经取得了长足的进步,这是副本中大多数答案的来源。

 const number = 12051852; const number2 = 88312; const number3 = 9; const number4 = 12345678910; const out = Intl.NumberFormat('es-ES').format(number); const out2 = Intl.NumberFormat('es-ES').format(number2); const out3 = Intl.NumberFormat('es-ES').format(number3); const out4 = Intl.NumberFormat('es-ES').format(number4); console.log(out); console.log(out2); console.log(out3); console.log(out4);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM