0

I have used the .00 currency filter from: AngularJS currency filter: can I remove the .00 if there are no cents in the amount?

However, how can i adapt the filter to split the value by comma's?

For example,

Instead of £456789 It should show: £456,789

Here's a plunker i made: http://plnkr.co/edit/uDFWkPAmc7PrgDwHPDho?p=preview

Filter:

app.filter('myCurrency', ['$filter', function ($filter) {


return function(input) {
    input = parseFloat(input);
    if(input % 1 === 0) {
      input = input.toFixed(0);
    }
    return '£' + input;
  };
}]);

2 Answers 2

1

Since you can chain filters, you could use something like the _.str trim filter:

jsFiddle

<p>My value with currency trimming filter: {{ myValue | currency:'&pound;' | _.str: 'trim':['.00'] }}</p>

result:

// My value with currency trimming filter: £242,737

In case you plan to support currencies as represented in different countries and so in case there's a comma ',' instead of a dot due to currency's l18n capability, you could consider a final version that includes both:

<p>My value with currency trimming filter: {{ myValue | currency:'&pound;' | _.str: 'trim':['.00'] | _.str: 'trim':[',00'] }}</p>
Sign up to request clarification or add additional context in comments.

Comments

0

Here is one way of doing it:

app.filter('myCurrency', ['$filter', function ($filter) {
  return function(input) {
    input = parseFloat(input);
    if(input % 1 === 0) {
      input = input.toFixed(0);
    }

    return '£' + (''+input).replace(/(\d)(?=(\d{3})+($|\.))/g, "$1,");
  };
}]);

Plunker

Edit

Updated filter to use a regex instead.

3 Comments

Thanks, but see what happens when you change myValue to 10000... Result with filter is £10,0
This circumvents the i18n power of Angular's currency filter. And quite convoluted solution.
@Plantface, First line of my post: "Here is one way of doing it". There are certainly many other ways of doing it... this is one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.