Html 在数字之间添加空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16637051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:33:10  来源:igfitidea点击:

Adding space between numbers

javascripthtmltextboxnumbersspace

提问by user1768788

I'm trying to make a number input. I've made so my textbox only accepts numbers via this code:

我正在尝试输入数字。我已经让我的文本框只通过这个代码接受数字:

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

But now the question comes to, how would I create spaces as the user is typing in their number, much like the iPhone's telephone spacing thing, but with numbers so for example if they type in: 1000 it will become 1 000;

但现在问题来了,当用户输入他们的号码时,我将如何创建空格,就像 iPhone 的电话间距一样,但是如果他们输入数字,例如:1000 它将变成 1000;

1
10
100
1 000
10 000
100 000
1 000 000
10 000 000
100 000 000
1 000 000 000

Etc...

等等...

I've tried to read the input from the right for each 3 characters and add a space. But my way is inefficient and when I'm changing the value from 1000 to 1 000 in the textbox, it selects it for some reason, making any key press after that, erase the whole thing.

我试图从右侧读取每 3 个字符的输入并添加一个空格。但是我的方法效率低下,当我将文本框中的值从 1000 更改为 1 000 时,它出于某种原因选择了它,之后按下任何键,擦除整个内容。

If you know how to do this, please refrain from using javascript plugins like jQuery. Thank You.

如果您知道如何执行此操作,请不要使用 jQuery 等 javascript 插件。谢谢你。

回答by Isham Mohamed

For integers use

对于整数使用

function numberWithSpaces(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}

For floating point numbers you can use

对于浮点数,您可以使用

function numberWithSpaces(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " ");
    return parts.join(".");
}

This is a simple regex work. https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions<< Find more about Regex here.

这是一个简单的正则表达式工作。https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions<< 在此处了解有关 Regex 的更多信息。

If you are not sure about whether the number would be integer or float, just use 2nd one...

如果您不确定数字是整数还是浮点数,只需使用第二个...

回答by Александр Соловей

Easiest way:

最简单的方法:

1

1

var num = 1234567890,
result = num.toLocaleString() ;// result will equal to "1 234 567 890"

2

2

var num = 1234567.890,
result = num.toLocaleString() + num.toString().slice(num.toString().indexOf('.')) // will equal to 1 234 567.890

3

3

var num = 1234567.890123,
result = Number(num.toFixed(0)).toLocaleString() + '.' + Number(num.toString().slice(num.toString().indexOf('.')+1)).toLocaleString()
//will equal to 1 234 567.890 123

4

4

If you want ',' instead of ' ':

如果你想要 ',' 而不是 ' ':

var num = 1234567.890123,
result = Number(num.toFixed(0)).toLocaleString().split(/\s/).join(',') + '.' + Number(num.toString().slice(num.toString().indexOf('.')+1)).toLocaleString()
//will equal to 1,234,567.890 123

If not working, set the parameter like: "toLocaleString('ru-RU')" parameter "en-EN", will split number by the ',' instead of ' '

如果不起作用,请设置参数如:“toLocaleString('ru-RU')”参数“en-EN”,将按','而不是''分割数字