简体   繁体   English

如何使用Javascript / jQuery将长数字字符串值转换为数值

[英]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: 我尝试了以下代码:

  1. Number($('#textbox1').val()).toFixed(2).replace(/\\B(?=(\\d{3})+(?!\\d))/g, ",")
  2. 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

Try using autoNumeric js. 尝试使用autoNumeric js。 Find it here 在这里找到

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.

相关问题 如何使用Javascript将字符串方程转换为数字值 - How to convert a string equation to a numeric value with Javascript 使用javascript / jquery从特定字符串获取数值 - Get a numeric value from specific string using javascript/jquery 如何使用javascript从字母数字字符串值中拆分数值? - how to split the numeric values from alphanumeric string value using javascript? 如何在javaScript中声明一个空的数字(数字)值 - how to declare an empty numeric (number) value in javaScript 如何从JavaScript中的字符串获取数值? - How to get a numeric value from a string in javascript? JavaScript使用Blob构造函数时会截取一个长数值 - JavaScript cuts a long numeric value while using blob constructor 如何将字符串值转换为Number以进行精确计算? Java脚本 - How to convert string value to Number for exact calculation ? Javascript 我如何将具有机器人数字和字母的字符串值转换为整数(整数) - how can i convert string value which has bot numeric and alphabet to whole number(integer) 如何在不使用任何外部库的情况下在 JavaScript 中将大数转换为字符串值? - How to convert big number to string value in JavaScript without using any external lib? JavaScript / jQuery:获取日期字符串的数字工作日值 - JavaScript / jQuery: get numeric weekday value of date string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM