使用Javascript计算小数点前后的位数

使用Javascript计算小数点前后的位数,javascript,asp.net,Javascript,Asp.net,我有一个要求,我应该允许小数点前最多14位,小数点后最多4位 有没有一种方法可以让用户知道他是否正在输入2222222.222——一旦他使用Javascript从文本框中跳出,小数点前15位是无效的 我试过了,但没用: MynewTextBox.Attributes.Add("onkeyup", "javascript:this.value=Comma(this.value);"); function Comma( Num ) { var pe

我有一个要求,我应该允许小数点前最多14位,小数点后最多4位

有没有一种方法可以让用户知道他是否正在输入2222222.222——一旦他使用Javascript从文本框中跳出,小数点前15位是无效的

我试过了,但没用:

  MynewTextBox.Attributes.Add("onkeyup", "javascript:this.value=Comma(this.value);");

function Comma( Num ) {

  var period = Num.indexOf('.'); 
   if ( Num.length > (period + 4)) 
   alert("too many after decimal point");
   if ( period != -1 ) 
   {
      Num += '00000'; 
      Num = Num.substr( 0, (period + 4));
   } 
此外,上面的函数给出了错误:

应为对象


有人能帮我吗。

使用正则表达式

萨马特式

pattern = /^\d{1,14)(\.{1,4}\)?$/;

if (patten.test(yourNumber)) {
// Hunky dory
}
else
{
// have another bash
}
为什么不使用该方法(下面是未测试的代码):

编辑
下面将获取任意数字,并返回一个在小数点前最多14位,在小数点后最多4位的数字(实际上,它并不验证输入是否为数字,但您可以看到图片):


(和往常一样,代码是未经测试的。)

我认为我们可以同意,将数字转换成字符串并计算小数点后的位数是很糟糕的,特别是考虑到非常大的数字可以转换成科学记数法

我不是一名计算机科学家,但我在JS中拼凑了一些东西,可以在数学上做到这一点。支持负数、科学记数法,可以解析大到10^308、小到10^-323的值

function countDigits(value) {
  if (value === 0) return { wholePlaces: 0, decimalPlaces: 0 };

  var absValue = Math.abs(value); // -15.555 becomes 15.555
  var wholePlaces = 0;
  for (; wholePlaces <= 308; ++wholePlaces) { // Number.MAX_VALUE is 1.798e+308
    if (absValue < Math.pow(10, wholePlaces))
      break;
  }

  var decimalValue = absValue - Math.floor(absValue); // 15.555 - 15 = 0.555
  var decimalPlaces = 0;
  for (; decimalPlaces >= -323; --decimalPlaces) { // Number.MIN_VALUE is 5e-324
    var temp = (decimalValue / Math.pow(10, decimalPlaces)) + 0.09; // Adding 0.09 to counter float errors
    if (temp - Math.floor(temp) < 0.1)  // If the decimal remaining is smaller that 0.1, we've reached the end
      break;
  }
  decimalPlaces = Math.abs(decimalPlaces);
  return {
    wholePlaces,
    decimalPlaces,
  }
}

countDigits(0);         // { wholePlaces: 0, decimalPlaces: 0 }
countDigits(0.10);      // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(-0.10);     // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(0.10000);   // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(-0.10000);  // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(5);         // { wholePlaces: 1, decimalPlaces: 0 }
countDigits(-5);        // { wholePlaces: 1, decimalPlaces: 0 }
countDigits(15.555);    // { wholePlaces: 2, decimalPlaces: 3 }
countDigits(-15.555);   // { wholePlaces: 2, decimalPlaces: 3 }
countDigits(215.555);   // { wholePlaces: 3, decimalPlaces: 3 }
countDigits(-215.555);  // { wholePlaces: 3, decimalPlaces: 3 }
countDigits(1.55555e+4) // { wholePlaces: 5, decimalPlaces: 1 } (15555.5)
函数计数位数(值){
if(value==0)返回{wholePlaces:0,decimalPlaces:0};
var absValue=Math.abs(值);//-15.555变为15.555
var wholePlaces=0;
对于(;wholePlaces=-323;--decimalPlaces){//Number.MIN_值为5e-324
var temp=(decimalValue/Math.pow(10,decimalPlaces))+0.09;//添加0.09以计数器浮点错误
if(temp-Math.floor(temp)<0.1)//如果剩余的小数点小于0.1,我们就到了末尾
打破
}
小数位数=Math.abs(小数位数);
返回{
整个地方,
小数点,
}
}
计数位数(0);//{wholePlaces:0,decimalPlaces:0}
计数位数(0.10);//{wholePlaces:0,decimalPlaces:1}
计数位数(-0.10);//{wholePlaces:0,decimalPlaces:1}
计数位数(0.10000);//{wholePlaces:0,decimalPlaces:1}
计数位数(-0.10000);//{wholePlaces:0,decimalPlaces:1}
计数位数(5);//{wholePlaces:1,decimalPlaces:0}
计数位数(-5);//{wholePlaces:1,decimalPlaces:0}
计数位数(15.555);//{wholePlaces:2,decimalPlaces:3}
计数位数(-15.555);//{wholePlaces:2,decimalPlaces:3}
计数位数(215.555);//{wholePlaces:3,decimalPlaces:3}
计数位数(-215.555);//{wholePlaces:3,decimalPlaces:3}
countDigits(1.55555e+4)/{wholePlaces:5,decimalPlaces:1}(15555.5)

+1,尽管它应该是
/^\d{1,14}(\.\d{1,4})$/
。我试图纠正它,但我认为你应该…我尝试了这个,它的工作很好,但不允许我纠正的数字。不断发出警报。有什么想法吗?你希望如何“纠正”这个数字?请记住,更正从用户输入中收到的数字总是很困难的,因为您不知道预期的数字是什么。好吧,一旦我单击警报的“确定”按钮,我就不能更正数字了……如果这是一个奇怪的问题,请道歉。我是javascript函数的新手。在if语句中,您可以对数字执行任何操作。。。让我给你举个例子。一般来说,如果答案中包含了对代码意图的解释,以及为什么在不介绍其他代码的情况下解决问题,那么答案会更有帮助。谢谢你提高了答案的参考价值,让它更容易理解!我尝试了countDigits(12222222.97777721),但失败了。
function Comma(num) {
  var s = num.split('.');
  var beforeDecimal = s[0];         // This is the number BEFORE the decimal.
  var afterDecimal = '0000';        // Default value for digits after decimal
  if (s.length > 1)                 // Check that there indeed is a decimal separator.
    afterDecimal = s[1];            // This is the number AFTER the decimal.
  if (beforeDecimal.length > 14) {
    // Too many numbers before decimal.
    // Get the first 14 digits and discard the rest.
    beforeDecimal = beforeDecimal.substring(0, 14);
  }
  if (afterDecimal.length > 4) {
    // Too many numbers after decimal.
    // Get the first 4 digits and discard the rest.
    afterDecimal = afterDecimal.substring(0, 4);
  }

  // Return the new number with at most 14 digits before the decimal
  // and at most 4 after.
  return beforeDecimal + "." + afterDecimal;
}
function countDigits(value) {
  if (value === 0) return { wholePlaces: 0, decimalPlaces: 0 };

  var absValue = Math.abs(value); // -15.555 becomes 15.555
  var wholePlaces = 0;
  for (; wholePlaces <= 308; ++wholePlaces) { // Number.MAX_VALUE is 1.798e+308
    if (absValue < Math.pow(10, wholePlaces))
      break;
  }

  var decimalValue = absValue - Math.floor(absValue); // 15.555 - 15 = 0.555
  var decimalPlaces = 0;
  for (; decimalPlaces >= -323; --decimalPlaces) { // Number.MIN_VALUE is 5e-324
    var temp = (decimalValue / Math.pow(10, decimalPlaces)) + 0.09; // Adding 0.09 to counter float errors
    if (temp - Math.floor(temp) < 0.1)  // If the decimal remaining is smaller that 0.1, we've reached the end
      break;
  }
  decimalPlaces = Math.abs(decimalPlaces);
  return {
    wholePlaces,
    decimalPlaces,
  }
}

countDigits(0);         // { wholePlaces: 0, decimalPlaces: 0 }
countDigits(0.10);      // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(-0.10);     // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(0.10000);   // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(-0.10000);  // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(5);         // { wholePlaces: 1, decimalPlaces: 0 }
countDigits(-5);        // { wholePlaces: 1, decimalPlaces: 0 }
countDigits(15.555);    // { wholePlaces: 2, decimalPlaces: 3 }
countDigits(-15.555);   // { wholePlaces: 2, decimalPlaces: 3 }
countDigits(215.555);   // { wholePlaces: 3, decimalPlaces: 3 }
countDigits(-215.555);  // { wholePlaces: 3, decimalPlaces: 3 }
countDigits(1.55555e+4) // { wholePlaces: 5, decimalPlaces: 1 } (15555.5)