karl124
karl124

Reputation: 41

Angularjs: parenthetically show negative numbers?

Is there a way to either: 1) filter currency numbers without any indication of which currency the number is (including a dollar sign; no symbols what-so-ever)? 2) filter regular numbers by showing negative numbers in parentheses?

E.g. -12.345 filtered would look like: (12.345)

Thank you.

Upvotes: 3

Views: 3450

Answers (2)

Anthony Chu
Anthony Chu

Reputation: 37520

The currency filter takes a symbol parameter. Set it to '' and it won't show a symbol. It'll show parentheses when negative...

{{ value | currency : '' }}

It will, however, round the number to 2 decimal places. You can easily create a custom filter...

app.filter('numeric', function() {
    return function (value) {
        if (value < 0) {
            value = '(' + Math.abs(value) + ')';
        }
        return value;
    };
});

JSFiddle

Upvotes: 4

aapierce
aapierce

Reputation: 897

You can use this filter to display a number with parenthesis instead of a minus sign:

angular.module('yourModule', [])
    .filter('negativeParenthesis', function() {
        return function(input) {
            if (input < 0) {
                return "("+Math.abs(input)+")";
            } else {
                return input + "";
            }
        }
    });

You would, of course, use it like so:

<p>{{myNumberValue|negativeParenthesis}}</p>

Upvotes: 0

Related Questions