AngularJS currency filter: can I remove the .00 if there are no cents in the amount?

后端 未结 3 881
闹比i
闹比i 2021-02-20 09:32

I am following this: http://docs.angularjs.org/api/ng.filter:currency When I type 1234.56 in the field then the output should be $1,234.56. But if I type in the input 1234 then

3条回答
  •  礼貌的吻别
    2021-02-20 09:57

    Add a new filter:

    'use strict';
    
    angular
        .module('myApp')
        .filter('myCurrency', ['$filter', function($filter) {
            return function(input) {
                input = parseFloat(input);
                input = input.toFixed(input % 1 === 0 ? 0 : 2);
                return '$' + input.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
            };
        }]);
    

    In your view:

    {{ '100' | myCurrency }} 
    {{ '100.05' | myCurrency }} 
    {{ '1000' | myCurrency }} 
    {{ '1000.05' | myCurrency }} 
    

提交回复
热议问题