Last active
January 14, 2018 23:53
-
-
Save rafaelcamargo/884327a77b3c0c6afd33b6aca4c2427d to your computer and use it in GitHub Desktop.
Brazilian Real currency filter for AngularJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script data-require="[email protected]" 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
window.app = angular.module('app', []); | |
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]); | |
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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