AngularJS如何使用货币过滤器?代码示例

2021年3月30日10:30:41 发表评论 661 次浏览

AngularJS货币过滤器用于将数字转换为货币格式。如果未指定货币格式, 则货币过滤器将使用本地货币格式。

语法如下:

{{ currency_expression | currency : symbol : fractionSize}}

参数:它包含上面提到和下面描述的两个参数:

  • 符号:它是可选参数。用于指定货币符号。货币符号可以是任何字符或文本。
  • 分数:它是可选参数。用于指定小数位数。

范例1:本示例以印度货币格式显示数字。

<!DOCTYPE html>
< html >
     < head >
         < title >Currency Filter</ title >
          
         < script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" >
         </ script >
     </ head >
      
     < body >
   
         < div ng-app = "gfgApp" ng-controller = "currencyCntrl" >
              
             < h3 >
                 Currency filter with curreency
                 symbol and fraction size.
             </ h3 >
              
             < p >Amount : {{ amount | currency : "Rs" : 2}}</ p >
         </ div >
   
         < script >
             var app = angular.module('gfgApp', []);
              
             app.controller('currencyCntrl', function($scope) {
                 $scope.amount = 75;
             });
         </ script >
     </ body >
      
</ html >

输出如下:

AngularJS如何使用货币过滤器?1

范例2:本示例以fromat货币形式显示数字。

<!DOCTYPE html>
< html >
     < head >
         < title >Currency Filter</ title >
          
         < script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" >
         </ script >
     </ head >
      
     < body >
   
         < div ng-app = "gfgApp" ng-controller = "currencyCntrl" >
              
             < h3 >
                 Currency filter without curreency
                 symbol and fraction size.
             </ h3 >
              
             < p >Amount : {{ amount | currency}}</ p >
         </ div >
   
         < script >
             var app = angular.module('gfgApp', []);
              
             app.controller('currencyCntrl', function($scope) {
                 $scope.amount = 38;
             });
         </ script >
   
     </ body >
</ html >

输出如下:

AngularJS如何使用货币过滤器?2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: