动态重新格式化 JavaScript 中的输入以向数字添加逗号

发布于 2024-11-03 16:57:02 字数 203 浏览 2 评论 0原文

我在输入数字时遇到问题。用户通常输入一个带有许多零的大数字, 而且经常会缺少一两个零,因为很难准确计数。

我认为 javascript 可以通过向用户显示他们输入的数字(用逗号格式化)来解决这个问题。

例如:

输入: | 1230000000000 |

结果:1,230,000,000,000

这是如何实现的?

I have an issue with number inputting. The user usually enters a large number with many zeros,
and often they are missing one or two zero as it is difficult to accurately count them.

I think javascript can work this out by showing the user the number they have inducted, formatted with commas.

eg:

input: | 1230000000000 |

Result: 1,230,000,000,000

How could this be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的他你的她 2024-11-10 16:57:02

中使用以下函数

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

在 javascript示例

addCommas('9999999.00')
// 9,999,999.00

Use the following function in javascript

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

example

addCommas('9999999.00')
// 9,999,999.00
囍笑 2024-11-10 16:57:02

这是一个老问题,但仍然没有正确的答案,所以,这是我的动态解决方案,它需要相同的 addCommas 函数来重新格式化输出,但添加一个 keyup 事件来清理当前值(删除 ',' )并再次重新格式化新值价值。

$('.datainto').keyup(function () {
    var value = $(this).val().replace(/,/g,'');
    $(this).val(addCommas(value));
});

在这里检查工作解决方案:
http://jsfiddle.net/darorck/371zrjet/

This is an old question but still without a correct answer so, this is my dynamic solution, it takes the same addCommas function to reformat the output but add a keyup event to clean the current value ( remove ',' ) and reformat again the new value.

$('.datainto').keyup(function () {
    var value = $(this).val().replace(/,/g,'');
    $(this).val(addCommas(value));
});

Check the working solution here:
http://jsfiddle.net/darorck/371zrjet/

近箐 2024-11-10 16:57:02
function formatNumber() {
  // select the input box
  let numInput = document.getElementById("myNumberInput");

  //get the value from the selected input box, remove commas
  //and convert it into float
  let num = parseFloat(numInput.value.replace(/,/g, ""));

  //replace the value in the input box with comma separated value
  //only if it is a valid number
  if (!isNaN(num)) {
    let formattedNum = num.toLocaleString('en-US');
    numInput.value = formattedNum;
  } else {
    numInput.value = "";
  }
}
<input type="text" id="myNumberInput" onkeyup="formatNumber()" />

function formatNumber() {
  // select the input box
  let numInput = document.getElementById("myNumberInput");

  //get the value from the selected input box, remove commas
  //and convert it into float
  let num = parseFloat(numInput.value.replace(/,/g, ""));

  //replace the value in the input box with comma separated value
  //only if it is a valid number
  if (!isNaN(num)) {
    let formattedNum = num.toLocaleString('en-US');
    numInput.value = formattedNum;
  } else {
    numInput.value = "";
  }
}
<input type="text" id="myNumberInput" onkeyup="formatNumber()" />

谈场末日恋爱 2024-11-10 16:57:02

在现代浏览器中,您只需使用 toLocaleString() 即可实现此目的

console.log((1230000000000).toLocaleString());
console.log((12300000000).toLocaleString());
console.log((1230000.152).toLocaleString());

我知道我给出答案已经很晚了,但是我仍然发布了这个答案,因为这个问题出现在 How to add adynamic comma in number in javascript 的搜索结果中,所以我想我需要添加一个对于即将到来的开发人员来说更短、更好的答案。

In modern browsers, you can simply achieve this with toLocaleString()

console.log((1230000000000).toLocaleString());
console.log((12300000000).toLocaleString());
console.log((1230000.152).toLocaleString());

I know I'm very late for giving the answer, But still, I post this answer because this question is coming in the search result of How to add a dynamic comma in number in javascript, So I thought I need to add an answer which is shorter and better for upcoming developers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文