Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Add precision for currency filter #5674

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*
* @param {number} amount Input to filter.
* @param {string=} symbol Currency symbol or identifier to be displayed.
* @param {(number|string)=} fractionSize Number of decimal places to round the number to.
* If this is not provided then the fraction size is computed from the current locale's number
* formatting pattern. In the case of the default locale, it will be 3.
* @returns {string} Formatted number.
*
*
Expand All @@ -26,27 +29,31 @@
<input type="number" ng-model="amount"> <br>
default currency symbol ($): {{amount | currency}}<br>
custom currency identifier (USD$): {{amount | currency:"USD$"}}
custom currency fractionSize (USD$): {{amount | currency:"USD$":3}}
</div>
</doc:source>
<doc:scenario>
it('should init with 1234.56', function() {
expect(binding('amount | currency')).toBe('$1,234.56');
expect(binding('amount | currency:"USD$"')).toBe('USD$1,234.56');
expect(binding('amount | currency:"USD$":3')).toBe('USD$1,234.560');
});
it('should update', function() {
input('amount').enter('-1234');
expect(binding('amount | currency')).toBe('($1,234.00)');
expect(binding('amount | currency:"USD$"')).toBe('(USD$1,234.00)');
expect(binding('amount | currency:"USD$":3')).toBe('(USD$1,234.000)');
});
</doc:scenario>
</doc:example>
*/
currencyFilter.$inject = ['$locale'];
function currencyFilter($locale) {
var formats = $locale.NUMBER_FORMATS;
return function(amount, currencySymbol){
return function(amount, currencySymbol, fractionSize){
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP,
fractionSize).
replace(/\u00A4/g, currencySymbol);
};
}
Expand Down Expand Up @@ -99,7 +106,6 @@ function currencyFilter($locale) {
</doc:example>
*/


numberFilter.$inject = ['$locale'];
function numberFilter($locale) {
var formats = $locale.NUMBER_FORMATS;
Expand All @@ -122,7 +128,7 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
var hasExponent = false;
if (numStr.indexOf('e') !== -1) {
var match = numStr.match(/([\d\.]+)e(-?)(\d+)/);
if (match && match[2] == '-' && match[3] > fractionSize + 1) {
if (match && match[2] == '-' && match[3] > (fractionSize || pattern.maxFrac) + 1) {
numStr = '0';
} else {
formatedText = numStr;
Expand Down
123 changes: 119 additions & 4 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,107 @@

describe('filters', function() {

var filter;

beforeEach(inject(function($filter){
var filter, locale;
beforeEach(module( function ($provide) {
locale = {
"DATETIME_FORMATS": {
"AMPMS": [
"AM",
"PM"
],
"DAY": [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
"MONTH": [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
"SHORTDAY": [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
],
"SHORTMONTH": [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
"fullDate": "EEEE, MMMM d, y",
"longDate": "MMMM d, y",
"medium": "MMM d, y h:mm:ss a",
"mediumDate": "MMM d, y",
"mediumTime": "h:mm:ss a",
"short": "M/d/yy h:mm a",
"shortDate": "M/d/yy",
"shortTime": "h:mm a"
},
"NUMBER_FORMATS": {
"CURRENCY_SYM": "$",
"DECIMAL_SEP": ".",
"GROUP_SEP": ",",
"PATTERNS": [
{
"gSize": 3,
"lgSize": 3,
"macFrac": 0,
"maxFrac": 3,
"minFrac": 0,
"minInt": 1,
"negPre": "-",
"negSuf": "",
"posPre": "",
"posSuf": ""
},
{
"gSize": 3,
"lgSize": 3,
"macFrac": 0,
"maxFrac": 2,
"minFrac": 2,
"minInt": 1,
"negPre": "(\u00a4",
"negSuf": ")",
"posPre": "\u00a4",
"posSuf": ""
}
]
},
"id": "en-us",
"pluralCat": function (n) { if (n == 1) { return PLURAL_CATEGORY.ONE; } return PLURAL_CATEGORY.OTHER;}
};
var PLURAL_CATEGORY = {ZERO: "zero", ONE: "one", TWO: "two", FEW: "few", MANY: "many", OTHER: "other"};
$provide.value("$locale", locale);
}));
beforeEach(inject(function($filter) {
filter = $filter;
}));

Expand All @@ -22,7 +120,7 @@ describe('filters', function() {
var pattern;

beforeEach(function() {
pattern = { minInt: 1,
pattern = {
minFrac: 0,
maxFrac: 3,
posPre: '',
Expand Down Expand Up @@ -109,6 +207,23 @@ describe('filters', function() {
expect(currency(0.008)).toBe('$0.01');
expect(currency(0.003)).toBe('$0.00');
});

it('should handle overriding the default locale fractionSize', function() {
expect(currency(0.008, "$", 3)).toBe('$0.008');
expect(currency(0.008, "$", 0)).toBe('$0');
});

it('should apply locale number format min and max fractionSize', function() {
locale.NUMBER_FORMATS.PATTERNS[1].maxFrac = 3;
locale.NUMBER_FORMATS.PATTERNS[1].minFrac = 2;

expect(currency(0.0008)).toBe('$0.001');
expect(currency(0.00)).toBe('$0.00');

//reset values for other tests
locale.NUMBER_FORMATS.PATTERNS[1].maxFrac = 2;
locale.NUMBER_FORMATS.PATTERNS[1].minFrac = 2;
});
});


Expand Down