Javascript 如何隐藏货币';s分数分量如果为零?

Javascript 如何隐藏货币';s分数分量如果为零?,javascript,angularjs,angularjs-filter,Javascript,Angularjs,Angularjs Filter,我希望分数分量显示在高于零的位置,但在等于零时隐藏。例如: {{123.45| currency:"USD$"}} 应该是123.45美元,但是 {{123 | currency:"USD$"}} 必须显示$123,而不是$123.00 我想你是说,如果分数部分大于零,你只希望分数部分可见。如果是这样,你可以用这个 {{amount | currency:"USD$": amount % 1 > 0 ? 2 : 0}} 我想你是说,如果分数部分大于零,你只希望分数部分可见。如果是这样

我希望分数分量显示在高于零的位置,但在等于零时隐藏。例如:

{{123.45| currency:"USD$"}}
应该是123.45美元,但是

{{123 | currency:"USD$"}}
必须显示$123,而不是$123.00


我想你是说,如果分数部分大于零,你只希望分数部分可见。如果是这样,你可以用这个

{{amount | currency:"USD$": amount % 1 > 0 ? 2 : 0}}

我想你是说,如果分数部分大于零,你只希望分数部分可见。如果是这样,你可以用这个

{{amount | currency:"USD$": amount % 1 > 0 ? 2 : 0}}

基于@Stafford Williams answer,我创建了一个自定义指令

另外,我对它进行了调整,以支持您不提供符号的情况

.filter('currencyWithoutZeros', ['$filter', function($filter) {
        return function() {
            // Clone arguments so we can modify it
            var argsCopy = Array.prototype.slice.call(arguments);

            var currencyFilter = $filter("currency"); 

            // If currency symbol was not provided, we need to manually add a second argument
            if (argsCopy.length == 1)
              argsCopy.push(undefined);

            var input = argsCopy[0]; 
            argsCopy.push(input % 1 > 0 ? 2 : 0);

            return currencyFilter.apply(undefined, argsCopy);
        };
    }]);
要使用它:

  fractions only if existing with custom directive: <span id="currency-no-fractions-custom">{{amount | currencyWithoutZeros:"USD$" }}</span><br/>
  fractions only if existing with custom directive (without symbol): <span id="currency-no-fractions-custom-no-symbol">{{amount | currencyWithoutZeros }}</span>
分数仅当与自定义指令一起存在时:{amount | currencywithout零:“USD$”}
分数仅当与自定义指令(无符号)一起存在时:{amount | currencywithout zeros}

基于@Stafford Williams answer,我创建了一个自定义指令

另外,我对它进行了调整,以支持您不提供符号的情况

.filter('currencyWithoutZeros', ['$filter', function($filter) {
        return function() {
            // Clone arguments so we can modify it
            var argsCopy = Array.prototype.slice.call(arguments);

            var currencyFilter = $filter("currency"); 

            // If currency symbol was not provided, we need to manually add a second argument
            if (argsCopy.length == 1)
              argsCopy.push(undefined);

            var input = argsCopy[0]; 
            argsCopy.push(input % 1 > 0 ? 2 : 0);

            return currencyFilter.apply(undefined, argsCopy);
        };
    }]);
要使用它:

  fractions only if existing with custom directive: <span id="currency-no-fractions-custom">{{amount | currencyWithoutZeros:"USD$" }}</span><br/>
  fractions only if existing with custom directive (without symbol): <span id="currency-no-fractions-custom-no-symbol">{{amount | currencyWithoutZeros }}</span>
分数仅当与自定义指令一起存在时:{amount | currencywithout零:“USD$”}
分数仅当与自定义指令(无符号)一起存在时:{amount | currencywithout zeros}

你是说你想在小数点存在时显示小数点,但小数点为零时不显示小数点?你是说你想在小数点存在时显示小数点,但小数点为零时不显示小数点?这是正确的,但每次我使用货币过滤器时,我都必须编写逻辑。即使我创建了customFilter来完成它。我正在寻找非常有用和优雅的方法,如何编写一个具有上述逻辑的自定义筛选器,这样您就可以使用
{amount | myCurrency:“USD$”}
?这是正确的,但每次使用currency filter时,我都必须编写一个逻辑。即使我创建了customFilter来完成它。我正在寻找非常有用和优雅的方法,如何编写一个具有上述逻辑的自定义过滤器,这样您就可以使用
{{amount | myCurrency:“USD$”}