如何管理角度滤波器的舍入?

时间:2015-07-14 05:56:39

标签: angularjs

我正在开发一个圆角为6位小数的角度滤波器。当小数只包含零时,我无法弄清楚如何舍入到2位小数。所以1.00000会转换为1.00。 这就是我的过滤器的样子:

app.filter('customCurrency',function ($filter) {
    return function (amount, currencySymbol,fractionSize) {
        var currency = $filter('currency');

        if (amount < 0) {
            return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
        }

        debugger;
        if (fractionSize !== undefined) {
            amount = currency(amount, currencySymbol, fractionSize);
        } else {
            amount = currency(amount, currencySymbol)
        }


        debugger;
        var amounts = amount.split(".");
        var amountHtml ;

         if (amounts[1].length==2 && amounts[1][0]==0 && amounts[1][1]==0)
         {
             amountHtml = amounts[0] + '<span class="decimals">.00</span>';
         }
        else
         {
             amountHtml= amounts[0] + '<span class="decimals">.' + amounts[1] + '</span>';

         }
        return amountHtml;
    };
});

plunkr:http://plnkr.co/edit/cGmYcsqwn3huanOeX4g4?p=preview

2 个答案:

答案 0 :(得分:0)

尝试这个

it('if I have 3 zeros or more in decimals only display max of 2 zeros in decimals', function() {
    // using $filter
    expect($filter('customCurrency')(1.000, '$', 2)).toEqual('$1<span class="decimals">.00</span>');
  });

答案 1 :(得分:0)

从长远来看,不确定这对你有用,因为我确定有更好的方法可以解决这个问题,但是如果你想要快速而肮脏的修复,请试试这个:

DEMO

app.filter('customCurrency',function ($filter) {

    return function (amount, currencySymbol, fractionSize) {

        var currency = $filter('currency');

        if (amount < 0) {
            return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
        }

        debugger;
        if (fractionSize !== undefined) {
            amount = currency(amount, currencySymbol, fractionSize);
        } else {
            amount = currency(amount, currencySymbol)
        }


        debugger;

        var amounts         = amount.split("."),
            fractionalPart  = amounts[1],
            decimals        = fractionalPart.length,
            zerosFromRight  = countZerosFromRight(fractionalPart);


        if(zerosFromRight > 2){
          return amounts[0] + '<span class="decimals">.00</span>';
        }

        return amounts[0] + '<span class="decimals">.' + amounts[1] + '</span>';
        ///////////////////////////////////

        function countZerosFromRight(str){

          var len   = str.length,
              count = 0;

          while(len--){

            if(str[len] === '0'){
              count++;
              continue;
            }

            break;
          }

          return count
        }


    };
});

修改 重新思考并认为这种方式更好:

我已经添加了此测试,也许您不需要此功能,但我认为这有点强大。

DEMO2

it('should only remove the zeros up to the last non-zero number', function() {
    expect($filter('customCurrency')(1.004000, '$', 6)).toEqual('$1<span class="decimals">.004</span>');
    expect($filter('customCurrency')(1.4066000, '$', 6)).toEqual('$1<span class="decimals">.4066</span>');
  });

<强> app.js

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

'use strict';

var app = angular.module('plunker');

app.filter('customCurrency',function ($filter) {

    return function (amount, currencySymbol, fractionSize) {

        var currency = $filter('currency');

        if (amount < 0) {
            return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
        }

        debugger;

        var rounded         = round(amount, fractionSize),
            currencyString  = currency(rounded, currencySymbol, fractionSize),
            amounts         = currencyString.split("."),

            integerPart     = amounts[0],
            fractionalPart  = amounts[1] || false,
            indexLastNonZero = indexLastNonZero(fractionalPart);

        // if only zeros after decimal then remove all but 2
        if(indexLastNonZero === -1){
          return integerPart + '<span class="decimals">.00</span>';
        }

        // if zeros and other numbers after decimal remove all trailing zeros
        if(indexLastNonZero > 1){
          return integerPart + '<span class="decimals">.' + fractionalPart.slice(0, indexLastNonZero + 1) + '</span>';
        }

        return integerPart;
        /////////////////////////////////////////////

        function round(str, decimals){
          var num = +str;

          return num.toFixed(decimals);
        }

        function indexLastNonZero(str){
          var len   = str.length;

          while(len--){

            if(str[len] !== '0'){
              return len;
            }
          }

          return -1;
        }

    };
});