Currency Filter In AngularJS

In this article you will learn about Currency Filter in AngularJS. The “currency” filter includs the “$” Dollar Symbol as default.

Introduction
 
In this article you will learn about Currency Filter in AngularJS.
 
 
AngularJs :

AngularJS is a JavaScript framework. It was originally developed by Hevery and Adam Abrons. Now it’s maintained by Google. AngularJS is lightweight and easy to learn. It is perfect in Single Page Application projects. AngularJS is open source, free of cost, and easy to handle.
 
 
Currency Filter
 
One of the filters in AngularJS is the Currency Filter. This “currency” filter includes the “$” Dollar Symbol as the default. So we can use the following code as the html template format of Currency Filter. 
  1. {{ currency_expression | currency : symbol : fractionSize}}  
Code
 
Html :
 
The following html code contains the root directory “ng-app” & Controller “ng-controller”. So you can create any name for it. The AngularJS currency filter contains the “$” Dollar symbol as default
  1. <div ng-app="CurrencyApp" ng-controller="CurrencyController">  
  2.    <input type="number" ng-model="amount">  
  3.    <h2>{{ amount | currency }}</h2>  
  4. </div>  
AngularJS
 
When we run the following code you will get the result as “$1000”. So you can change this “$” Dollar symbol based on the html template format of Currency Filter.
  1. var myApp = angular.module('CurrencyApp', []);  
  2. myApp.controller('CurrencyController'function($scope) {  
  3.   $scope.amount = 1000;  
  4. });  
Demo: Currency Filter In AngularJS
 
Removing Default Symbol Of Currency Filter 
 
Html
 
In the following code value=”” , So the default Symbol “$” Dollar will be removed. 
  1. <div ng-app="CurrencyApp" ng-controller="CurrencyController">  
  2.       
  3.    <input type="number" ng-model="amount">  
  4.    <h2>{{ amount | currency : value="" }}</h2>  
  5.    <!-- or-->  
  6.    <h2>{{ amount | currency : "" }}</h2>  
  7.    
  8. </div>  
Demo: Removing the default symbol ( $ ) of currency filter in AngularJS 
 
List Of Countries and Their Currency Using Angular Js 
 
Html
 
The following code contains the currency symbol for respective countries. 
  1. <h3>Countries & Their Currency Using AngularJS</h3>  
  2. <div ng-app="CurrencyApp" ng-controller="CurrencyController"> <input type="number" ng-model="amount">  
  3.     <h3>    
  4.    Removed default currency($) :  {{ amount | currency : "" }}      
  5. </h3>  
  6.     <h3>    
  7.    Default currency($) :  {{ amount | currency }}      
  8. </h3>  
  9.     <h3>    
  10.    Indian Currency: {{amount | currency:"₹"}}    
  11. </h3>  
  12.     <h3>    
  13.    US Dollar - USD :  {{ amount | currency : "$" }}      
  14. </h3>  
  15.     <h3>    
  16.    United Kingdom Pound - GBP : {{amount | currency:"£"}}    
  17. </h3>  
  18.     <h3>    
  19.    Thailand Baht - THB : {{amount | currency:"฿"}}    
  20. </h3>  
  21.     <h3>    
  22.    China Yuan Renminbi - CNY : {{amount | currency:"¥"}}    
  23. </h3>  
  24.     <h3>    
  25.    Costa Rica Colon - CRC : {{amount | currency:"₡"}}    
  26. </h3>  
  27.     <h3>    
  28.     Ukraine Hryvnia - UAH  : {{amount | currency:"₴"}}    
  29. </h3> </div>    
AngularJS
  1. var myApp = angular.module('CurrencyApp', []);  
  2. myApp.controller('CurrencyController'function($scope) {  
  3.   $scope.amount = 1000;  
  4. });  
Demo: Countries & Their Currency Using AngularJS
 
Fraction Size In AngularJS Currency Filter 
 
We can use the fraction size in the currency filter. In the following code we added 3 as the fraction size so it added 3 zeros in the result. 
 
Html  
  1. <h3>Fraction Size In AngularJS Currency Filter</h3>  
  2.    
  3. <div ng-app="CurrencyApp" ng-controller="CurrencyController">  
  4.   <input type="number" ng-model="amount">  
  5. <h3>  
  6.     Amount :  {{ amount | currency : "$" : 3 }}    
  7. </h3>  
  8. </div>  
Demo: Fraction Size In AngularJS Currency Filter
 
Reference
Summary
 
We learned how to use Currency Filter In AngularJS. I hope this article is useful for all beginners.
 
Read more articles on AngularJS: