[英]How to convert long number string value to numeric value using Javascript / jQuery
User is allowed to insert up to 25 value in textbox
. 允许用户在textbox
最多插入25个值。
i.e. 1234567890123456789012345
On change event of textbox I need to convert that same into numeric value to perform numeric operations and then after need to display same entered value with thousand separator along with 2 decimal places. 在文本框的更改事件中,我需要将其转换为数值以执行数字运算,然后在显示相同的输入值后用千位分隔符和2个小数位显示。
I tried the following code: 我尝试了以下代码:
Number($('#textbox1').val()).toFixed(2).replace(/\\B(?=(\\d{3})+(?!\\d))/g, ",")
parseInt($('#textbox1').val(), 10).toFixed(2).replace(/\\B(?=(\\d{3})+(?!\\d))/g, ",")
Both gives me following output 两者都给我以下输出
1.2,345,679,801,234,568e+21
expected output:- 预期产量:
1,234,567,890,123,456,789,012,345.00
something like this work for you? 像这样的东西适合您? I found it on the internet... 我在互联网上找到的...
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 + '.00';
}
for the numeric point of view, still i could not get any solution at JavaScript, so I have posted the value to server side and get JSon result. 从数字的角度来看,我仍然无法在JavaScript上获得任何解决方案,因此我已将值发布到服务器端并获得JSon结果。
But for the display purpose I have did finally : 但是出于显示目的,我终于做到了:
function ChangeAmountFormat(target) {
var $this = $(target);
var num = $this.val().replace(/,/g, '').replace(/(\s)/g, '');
var decimalPointDeleted = '';
if (lastValue.length > 0 && lastValue.indexOf('.') > 0 && num.indexOf('.') < 0) {
decimalPointDeleted = 'y';
}
if (num.indexOf('.') >= 0) {
var numSplitValues = num.split('.');
num = numSplitValues[0].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
num += (num.length > 0 ? ('.' + numSplitValues[1]) : '');
}
else {
if (decimalPointDeleted == 'y' && num.toString().substring((num.length - 2), num.length) == '00') {
var tempNum = num.toString().substring(0, (num.length - 2));
num = tempNum + '.' + num.toString().substring((num.length - 2), num.length);
}
else {
num = num + (num.length > 0 ? '.00' : '');
}
num = num.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
$this.val(num);
}
here, some more things also handles. 在这里,更多的事情也可以处理。 Kindly ignore. 请忽略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.