Removing AngularJS currency filter decimal/cents

80,982

Solution 1

Update: as of version 1.3.0 - currencyFilter: add fractionSize as optional parameter, see commit and updated plunker

{{10 | currency:undefined:0}}

Note that it's the second parameter so you need to pass in undefined to use the current locale currency symbol

Update: Take note that this only works for currency symbols that are displayed before the number. As of version 1.2.9 it's still hardcoded to 2 decimal places.

Here is a modified version that uses a copy of angular's formatNumber to enable 0 fractionSize for currency.


Normally this should be configurable either in the locale definition or the currencyFilter call but right now(1.0.4) it's hardcoded to 2 decimal places.

Custom filter:

myModule.filter('noFractionCurrency',
  [ '$filter', '$locale',
  function(filter, locale) {
    var currencyFilter = filter('currency');
    var formats = locale.NUMBER_FORMATS;
    return function(amount, currencySymbol) {
      var value = currencyFilter(amount, currencySymbol);
      var sep = value.indexOf(formats.DECIMAL_SEP);
      if(amount >= 0) { 
        return value.substring(0, sep);
      }
      return value.substring(0, sep) + ')';
    };
  } ]);

Template:

<div>{{Price | noFractionCurrency}}</div>

Example:

Update: fixed a bug when handling negative values

Solution 2

The question seems to be pretty old and the given answers are nice. However, there is another alternative solution which can also help (which I use in my projects).

This is working very well with currency symbols prefixing as well as suffixing the number for positive and negative values.

Custom filter:

angular.module('your-module', [])
    .filter('nfcurrency', [ '$filter', '$locale', function ($filter, $locale) {
        var currency = $filter('currency'), formats = $locale.NUMBER_FORMATS;
        return function (amount, symbol) {
            var value = currency(amount, symbol);
            return value.replace(new RegExp('\\' + formats.DECIMAL_SEP + '\\d{2}'), '')
        }
    }])

Template:

<div>{{yourPrice| nfcurrency}}</div>

Examples for different locales:

  • 10.00 (en-gb) -> £10
  • 20.00 (en-us) -> $20
  • -10.00 (en-us) -> ($10)
  • 30.00 (da-dk) -> 30 kr
  • -30.00 (da-dk) -> -30 kr

Please have a look at live demo for US dollars and Danish Krone.

Update

Please note that this workaround is good for AngularJS 1.2 and earlier releases of the library. As of AngularJS 1.3 you can use currency formatter with third parameter specifying fraction size - "Number of decimal places to round the amount to".

Note that in order to use default currency format coming from AngularJS localization, you would have to use currency symbol (second parameter) set to undefined (null or empty will NOT work). Example in demos for US dollars and Danish Krone.

Solution 3

Another thing that is worth considering is if you know you only have one locale or one type of currency, you could put the currency symbol before the number and then use the number filter like so (for US currency).

  ${{Price | number:0}}

More of a quick fix solution if you don't want to throw in a new filter and only have one currency.

Solution 4

It's late but might be it can help some one

{{value | currency : 'Your Symbol' : decimal points}}

So let's see some examples with output

{{10000 | currency : "" : 0}}           // 10,000
{{10000 | currency : '$' : 0}}          // $10,000 
{{10000 | currency : '$' : 2}}          // $10,000.00 
{{10000 | currency : 'Rs.' : 2}}        // Rs.10,000.00
{{10000 | currency : 'USD $' : 2}}      // USD $10,000.00
{{10000 | currency : '#' : 3}}          // #10,000.000
{{10000 | currency : 'ANYTHING: ' : 5}} // ANYTHING: 10,000.00000

See the demo

Solution 5

This is another similar solution, but it removes .00 decimal, but leaves any other decimal amount.

$10.00 to $10

$10.20 to $10.20

app.filter('noFractionCurrency', [ '$filter', '$locale', function(filter, locale) {
    var currencyFilter = filter('currency');
    var formats = locale.NUMBER_FORMATS;
    return function(amount, currencySymbol) {
        amount = amount ? (amount*1).toFixed(2) : 0.00;
        var value = currencyFilter(amount, currencySymbol);
        // split into parts
        var parts = value.split(formats.DECIMAL_SEP);
        var dollar = parts[0];
        var cents = parts[1] || '00';
            cents = cents.substring(0,2)=='00' ? cents.substring(2) : '.'+cents; // remove "00" cent amount
        return dollar + cents;
    };
}]);
Share:
80,982
Mike Pateras
Author by

Mike Pateras

Young developer, interested in gaming and game development, along with many other things.

Updated on July 05, 2022

Comments

  • Mike Pateras
    Mike Pateras almost 2 years

    Is there a way to remove the decimal/cents from the output of a currency filter? I'm doing something like this:

    <div>{{Price | currency}}</div>
    

    Which outputs:

    $1,000.00

    Instead, I'd like:

    $1,000

    Can that be done using the currency filter? I know I can prepend a dollar sign onto a number, and I could write my own filter, but I was hoping a simple method exists with the existing currency filter.

    Thank you.