简体   繁体   English

Angularjs避免使用数字滤波器的科学记数法

[英]Angularjs avoid scientific notation for number filter

I need to print very small numbers for my app. 我需要为我的应用打印非常小的数字。 I prefer if they were all displayed in decimal format. 如果它们都以十进制格式显示,我更喜欢。 Is there a way to do this with the angularjs number filter or do I have to write my own or modify the existing one somehow? 有没有办法用angularjs数字过滤器做这个或者我必须自己编写或以某种方式修改现有的?

http://jsfiddle.net/ADukg/2386/ http://jsfiddle.net/ADukg/2386/

Javascript 使用Javascript

var myApp = angular.module('myApp',[]);

 function MyCtrl($scope) {
   $scope.MyNum1 = 0.000001;
   $scope.MyNum2 = 0.0000001;
 }

HTML HTML

<div ng-controller="MyCtrl">
  Small Number: {{MyNum1 | number:8}}<br/>
  Very Small Number: {{MyNum2 | number:8}}
</div>

Inconsistent Output 输出不一致

Small Number: 0.00000100
Very Small Number: 1e-7

I haven't fully tested this and I'm not sure how to get this working on jsfiddle, but this seems to be working for the time being. 我还没有完全测试过这个,我不知道如何让这个工作在jsfiddle,但这似乎暂时有效。

Javascript 使用Javascript

var app = angular.module('myApp', []);

app.filter('decimalFormat', function () {
  return function (text) {
    return parseFloat(text).toFixed(20).replace(/0+$/,'');
  };
});

HTML HTML

<div ng-controller="MyCtrl">
  Small Number: {{MyNum1 | number:8 | decimalFormat}}<br/>
  Very Small Number: {{MyNum2 | number:8 | decimalFormat}}
</div>

Edit after Langdon's comment, added commas: 兰登评论后编辑,加上逗号:

Javascript 使用Javascript

var app = angular.module('myApp', []);

app.filter('decimal', function () {
  return function (text) {
      var parts = parseFloat(text).toFixed(8).split('.');
      parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      return parts.join('.');
    }
  };
});

function MyCtrl($scope) {
  $scope.MyNum1 = 0.12345678912;
  $scope.MyNum2 = 0.000000100001;
  $scope.MyNum3 = 0;
  $scope.MyNum3 = 1123123.05;
}

HTML HTML

<div ng-controller="MyCtrl">
  Small Number: {{MyNum1 | decimal}}<br/>
  Very Small Number: {{MyNum2 | decimal}}<br/>
  Zero: {{MyNum3 | decimal}}<br/>
  Million: {{MyNum4 | decimal}}<br/>
</div>

You're going to have to write your own since the existing filter in the source doesn't handle large decimal places (see formatNumber in the filters.js ). 你将不得不写自己的,因为在源现有的过滤器不处理大小数位(见formatNumberfilters.js )。 Ultimately the problem is that toString is called to display the number in your HTML. 最终问题是调用toString来显示HTML中的数字。 More ideally, toFixed could be called, but that gets tricky because you have to figure out the length of the decimals. 更理想的是,可以调用toFixed ,但这很棘手,因为你必须弄清楚小数的长度。

console.log(0.000001);
 > 0.000001
console.log(0.0000001);
 > 1e-7
console.log(0.0000001.toFixed(20));
 > 0.00000010000000000000

Here's a quick hack: 这是一个快速的黑客:

console.log(0.0000001.toFixed(20).replace(/0+$/, ''));
 > 0.0000001

So your custom filter could be as simple as that. 所以你的自定义过滤器就这么简单。 I'd recommending filing an issue on GitHub though. 我建议在GitHub上提交一个问题。

This was made to return large numbers as decimal strings, but it works as well for small ones- 这是为了将数字作为十进制字符串返回,但它也适用于小字符串 -

Number.prototype.noExponents= function(){
    var data= String(this).split(/[eE]/);
    if(data.length== 1) return data[0]; 

    var  z= '', sign= this<0? '-':'',
    str= data[0].replace('.', ''),
    mag= Number(data[1])+ 1;

    if(mag<0){
        z= sign + '0.';
        while(mag++) z += '0';
        return z + str.replace(/^\-/,'');
    }
    mag -= str.length;  
    while(mag--) z += '0';
    return str + z;
}

var n=1e-7;
n.noExponents();
returned value: (String) '0.0000001';

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

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