简体   繁体   English

使用Javascript与Microsoft Excel一样对RoundUP起作用

[英]Function to RoundUP as Microsoft Excel does, in Javascript

Days ago, I started a project in which i needed to do some measure calculation; 几天前,我启动了一个项目,需要进行一些度量计算。 specifically: statistic calculation. 具体来说:统计计算。 Well, thats not the problem, the problem came with rounding floating numbers (numbers with decimals), and the way javascript deals with it. 好吧,那不是问题,问题在于舍入浮点数(带小数的数字),以及javascript处理它的方式。 I found some approaches in internet to rounding javascript numbers, but none of them was great alternatives to what exactly ms excel does (ROUND.MAX function). 我在互联网上找到了一些舍入javascript数字的方法,但是没有一种方法可以完全替代ms excel的功能(ROUND.MAX函数)。 So I have to recreate that function behavior by myself. 因此,我必须自己重新创建该功能行为。 So this is the code: 所以这是代码:

MathExt = {

 init: function(options){
  this.separator = options.separator || ",";
 },

 roundMax: function(number, decimals){
  var $_indexOf = number.lastIndexOf(this.separator);
  var fp = number.substring(0, $_indexOf);
  var rp = number.substr($_indexOf + 1);
  var roundedNumber = "";
  if(decimals > 0){
   var i = 0;
   var _pq = 0;
   var _zeros = "";

   while(rp.substr(i++, 1) == "0"){ _zeros += "0"; }

   i = rp.length - 1;
   while(i >= decimals){
    var _t = rp.charAt(i);
    rp = rp.substring(0, i);
    if(_t != "0"){
     if(rp.charAt(i - 1) == "9"){ _pq  = 1; }
     else{ 
      if(_pq == 1) _pq = 0;
      rp = _zeros + (parseInt(rp, 10) + 1) + "";
      if(i == _zeros.length) _zeros = _zeros.substr(0, i - 1);
     }
    }
    i--;
   }
   if(_pq == 0)
    roundedNumber = fp + this.separator + rp;
   else
    if(i == 0){
     roundedNumber = "" + (parseInt(fp) + 1);
    }
    else{
     rp = (parseInt(rp) + 1) + "";
     i = rp.length - 1;
     while(i > 0 && rp.substr(i, 1) == "0" ) rp = rp.substr(0, i);
     roundedNumber = fp + this.separator +  rp;
    }
  }
  else{
   var _s = rp.substring(0, 1);
   if(_s != "0") fp = "" + (parseInt(fp) + 1);
   roundedNumber = fp;
  }
  return roundedNumber;
 }
};

Well, this code works as I expeted with javascript numbers. 好吧,这段代码在我使用javascript数字时也能正常工作。 but Surpringly javascript truncate long floating numbers. 但令人惊讶的是javascript截断了长浮点数。 And the next is to do the same but with really long numbers. 第二个是做同样的事情,但是要有很长的数字。 Yesterday someone gave me a link which I found really useful, but. 昨天有人给我一个链接,我觉得它确实有用,但是。 I wanna know if this function (ROUND.MAX from EXCEL) is already implemented for this kind of big floating numbers. 我想知道此功能(EXCEL的ROUND.MAX)是否已针对这种大浮点数实现。

This is an example: 这是一个例子:

piii = 3.14159141597; for(var i = 0; i <= 11; i++){ console.log((Math.round((piii*Math.pow(10, i)))/Math.pow(10,i))); } extracted this from internet.
3
3.1
3.14
3.142
3.1416
3.14159
3.141591
3.1415914
3.14159142
3.141591416
3.141591416
3.14159141597

MS Excel ROUND.MAX FUNCTION
4
3,2
3,15
3,142
3,1416
3,1416
3,141592
3,1415915
3,14159142
3,141591416
3,141591416
3,14159141597

My first version of the ROUND.MAX algorithm
4
3.2
3.15
3.142
3.1416
3.1416
3.141592
3.1415915
3.14159142
3.141591416
3.141591416
3.14159141597

Just replace round with ceil in your example to get the roundmax effect: 只要在您的示例中将round替换为ceil即可获得roundmax效果:

piii = 3.14159141597;
for(var i = 0; i <= 11; i++) { 
    console.log((Math.ceil((piii*Math.pow(10, i)))/Math.pow(10,i)));
}

Output: 输出:

4
3.2
3.15
3.142
3.1416
3.1416
3.141592
3.1415915
3.14159142
3.141591416
3.141591416
3.14159141597

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM