Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AngularJS currency filter formats negative numbers with parenthesis?

Live Demo

Why this:

# Controller $scope.price = -10;  # View {{ price | currency }} 

results in ($10.00) rather than -$10.00?

like image 244
Misha Moroshko Avatar asked Jul 03 '13 06:07

Misha Moroshko


People also ask

Which AngularJS filter formats a number to a currency format?

AngularJS currency Filter The currency filter formats a number to a currency format. By default, the locale currency format is used.

What does the AngularJS filter currency do?

AngularJS currency filter is used to convert a number into a currency format. If no currency format is specified currency filter uses the local currency format.

Which built in filter is used to format the expression as currency in AngularJS?

AngularJS built-in currency filter accepts two optional parameters, those are currency Symbol and fraction size of currency. The dollar is a generic symbol of currency filter provided by the AngularJS framework.

How do you show negative numbers in currency?

The standard accounting way is always to show negative numbers in parentheses. If you want to appeal to primarily financial professionals, that's the accepted practice. She also cautions against using red or drawing attention to a negative number.


2 Answers

I know this is an old question, but the accepted answer is only answering why this happens, without a concrete solution to the problem. I think the "most correct way" of doing this, is to use a decorator like so:

angular     .module('app')     .config(['$provide', function($provide) {         $provide.decorator('$locale', ['$delegate', function($delegate) {           if($delegate.id == 'en-us') {             $delegate.NUMBER_FORMATS.PATTERNS[1].negPre = '-\u00A4';             $delegate.NUMBER_FORMATS.PATTERNS[1].negSuf = '';           }           return $delegate;         }]);       }]); 

This is only called once, valid for any filter which depends on it, and you don't need a custom filter for your currency formatting.

like image 199
marc Avatar answered Sep 18 '22 14:09

marc


This is a popular way to represent negative currencies. Wikipedia:

In bookkeeping, amounts owed are often represented by red numbers, or a number in parentheses, as an alternative notation to represent negative numbers.

You can see in the Angular source code where they do this (negSuf/negPre):

function $LocaleProvider(){   this.$get = function() {     return {       id: 'en-us',        NUMBER_FORMATS: {         DECIMAL_SEP: '.',         GROUP_SEP: ',',         PATTERNS: [           { // Decimal Pattern             minInt: 1,             minFrac: 0,             maxFrac: 3,             posPre: '',             posSuf: '',             negPre: '-',             negSuf: '',             gSize: 3,             lgSize: 3           },{ //Currency Pattern             minInt: 1,             minFrac: 2,             maxFrac: 2,             posPre: '\u00A4',             posSuf: '',             negPre: '(\u00A4',             negSuf: ')',             gSize: 3,             lgSize: 3           }         ],         CURRENCY_SYM: '$'       }, 
like image 44
Ryan Cavanaugh Avatar answered Sep 21 '22 14:09

Ryan Cavanaugh