DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Currency Filter in AngularJS

Currency Filter in AngularJS

In this article, I will show you some examples of how to use Currency Filter in AngularJS.

Rajeesh Menoth user avatar by
Rajeesh Menoth
·
May. 04, 16 · Web Dev Zone · Tutorial
Like (2)
Save
Tweet
30.28K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In this article, you will learn about Currency Filter in AngularJS.

Image title

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 called the Currency Filter. This “currency” filter includes the “$” Dollar Symbol as default. So, we can use the following code for the HTML template format of Currency Filter.

{{ currency_expression | currency : symbol : fractionSize}}

Code 

HTML

The following HTML code contains the root directory “ng-app” and Controller “ng-controller”. So you can create any name for it. The AngularJS currency filter contains the “$” Dollar symbol as default.

<div ng-app="CurrencyApp" ng-controller="CurrencyController">
   <input type="number" ng-model="amount">
   <h2>{{ amount | currency }}</h2>
</div>

AngularJS

When we run the following code we will get the result as “$1000”. So you can change this “$” Dollar symbol based on the HTML template format of Currency Filter.

var myApp = angular.module('CurrencyApp', []);
myApp.controller('CurrencyController', function($scope) {
  $scope.amount = 1000;
});

Demo: https://jsfiddle.net/rajeeshmenoth/g5n2x6c6/1/

Removing Default Symbol of Currency Filter

HTML

In the following code value = “ ” , so the default Symbol “$” Dollar will be removed.

<div ng-app="CurrencyApp" ng-controller="CurrencyController">

   <input type="number" ng-model="amount">
   <h2>{{ amount | currency : value="" }}</h2>
   <!-- or-->
   <h2>{{ amount | currency : "" }}</h2>

</div>

Demo: https://jsfiddle.net/rajeeshmenoth/g5n2x6c6/2/

List of Countries and Their Currency Using AngularJS

HTML

The following code contains the currency symbol for respective countries.

<h3>Countries & Their Currency Using AngularJS</h3>
<div ng-app="CurrencyApp" ng-controller="CurrencyController">
  <input type="number" ng-model="amount">
<h3>
   Removed default currency($) :  {{ amount | currency : "" }}  
</h3>
<h3>
   Default currency($) :  {{ amount | currency }}  
</h3>
<h3>
   Indian Currency: {{amount | currency:"&#8377"}}
</h3>
<h3>
   US Dollar - USD :  {{ amount | currency : "$" }}  
</h3>
<h3>
   United Kingdom Pound - GBP : {{amount | currency:"£"}}
</h3>
<h3>
   Thailand Baht - THB : {{amount | currency:"฿"}}
</h3>
<h3>
   China Yuan Renminbi - CNY : {{amount | currency:"¥"}}
</h3>
<h3>
   Costa Rica Colon - CRC : {{amount | currency:"₡"}}
</h3>
<h3>
    Ukraine Hryvnia - UAH  : {{amount | currency:"₴"}}
</h3>
</div>

AngularJS

var myApp = angular.module('CurrencyApp', []);
myApp.controller('CurrencyController', function($scope) {
  $scope.amount = 1000;
});

Demo: https://jsfiddle.net/rajeeshmenoth/g5n2x6c6/5/

Fraction Size in AngularJS Currency Filter

We can use the fraction size in currency filter.The following code we added 3 as the fraction size so it added 3 zeros in result.

HTML

<h3>Fraction Size In AngularJS Currency Filter</h3>

<div ng-app="CurrencyApp" ng-controller="CurrencyController">
  <input type="number" ng-model="amount">
<h3>
    Amount :  {{ amount | currency : "$" : 3 }}  
</h3>
</div>

Demo: https://jsfiddle.net/rajeeshmenoth/g5n2x6c6/6/

References

https://docs.angularjs.org/api/ng/filter/currency

http://www.w3schools.com/angular/angular_filters.asp

Summary

We learned how to use the Currency Filter in AngularJS. I hope this article is useful for all beginners.

AngularJS Filter (software)

Published at DZone with permission of Rajeesh Menoth, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • SDLC Vs STLC: What's the Difference?
  • How Does the Database Understand and Execute Your Query?
  • A First Look at CSS When and Else Statements
  • How To Deploy Apache Kafka With Kubernetes

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo