首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Javascript计算小数位前后的位数

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

Stack Overflow用户
提问于 2011-08-13 06:09:22
回答 4查看 10.4K关注 0票数 4

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

有没有一种方法可以让用户知道,一旦他使用Javascript离开文本框,他输入的是222222222222222.222 --小数之前的15位数字是无效的。

我试过了,但它对我没有帮助:

代码语言:javascript
复制
  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));
   } 

另外,上面的函数给了我一个错误:

应为

对象。

有没有人能帮我。

EN

回答 4

Stack Overflow用户

发布于 2011-08-13 06:20:32

为什么不使用split()方法(下面未经测试的代码):

代码语言:javascript
复制
function Comma(num) {
  var s = num.split('.');
  if (s[0].length > 14) {
    // Too many numbers before decimal.
  }
  if (s[1].length > 4) {
    // Too many numbers after decimal.
  }
}

编辑

下面的代码将接受任何数字,并返回一个小数点前至多14位,小数点后至多4位的数字(它实际上并不验证输入的是一个数字,但您得到了图像):

代码语言:javascript
复制
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;
}

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

票数 6
EN

Stack Overflow用户

发布于 2011-08-13 06:16:00

使用正则表达式

类总和

代码语言:javascript
复制
pattern = /^\d{1,14)(\.{1,4}\)?$/;

if (patten.test(yourNumber)) {
// Hunky dory
}
else
{
// have another bash
}
票数 5
EN

Stack Overflow用户

发布于 2018-06-04 07:40:14

我认为我们可以同意,将数字转换为字符串并计算小数点左边的数字是非常粗俗的,特别是考虑到非常大的数字可以转换为科学记数法。

我不是计算机科学家,但这里有一些我在JS中拼凑起来的东西,它将在数学上做到这一点。支持负数、科学记数法,可以解析10^308到10^323的值。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7046856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档