Skip to content

Instantly share code, notes, and snippets.

@rafaelcamargo
Last active January 14, 2018 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelcamargo/884327a77b3c0c6afd33b6aca4c2427d to your computer and use it in GitHub Desktop.
Save rafaelcamargo/884327a77b3c0c6afd33b6aca4c2427d to your computer and use it in GitHub Desktop.
Brazilian Real currency filter for AngularJS
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.6.0" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
</head>
<body ng-app="app">
<div ng-controller="appCtrl as ctrl">
<div>
<p>Basic usage</p>
<p ng-bind="ctrl.amount | brlCurrency"></p>
</div>
<div>
<p>Options (symbol, precision)</p>
<p ng-bind="ctrl.amount | brlCurrency : 'R$' : 3"></p>
</div>
</div>
<script src="app.js"></script>
<script src="controller.js"></script>
<script src="brl-currency-filter.js"></script>
</body>
</html>
(function(){
window.app = angular.module('app', []);
}());
(function(){
function brlCurrencyFilter($filter){
return function(amount, symbol, precision){
symbol = symbol ? [symbol,''].join(' ') : '';
precision = precision ? precision : 2;
amount = $filter('currency')(amount, symbol, precision);
return amount
.replace(/\./g,'DECIMAL')
.replace(/,/g,'.')
.replace(/DECIMAL/g,',');
};
}
app.filter('brlCurrency', ['$filter', brlCurrencyFilter]);
}());
(function(){
function appController(){
var _public = this;
_public.amount = 9815635.451813;
}
app.controller('appCtrl', appController);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment