Saturday, 15 February 2014

Removing AngularJS currency filter decimal/cents -



Removing AngularJS currency filter decimal/cents -

is there way remove decimal/cents output of currency filter? i'm doing this:

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

which outputs:

$1,000.00

instead, i'd like:

$1,000

can done using currency filter? know can prepend dollar sign onto number, , write own filter, hoping simple method exists existing currency filter.

thank you.

update: of version 1.3.0 - currencyfilter: add together fractionsize optional parameter, see commit , updated plunker

{{10 | currency:undefined:0}}

note it's sec parameter need pass in undefined utilize current locale currency symbol

update: take note works currency symbols displayed before number. of version 1.2.9 it's still hardcoded 2 decimal places.

here modified version uses re-create of angular's formatnumber enable 0 fractionsize currency.

normally should configurable either in locale definition or currencyfilter phone call right now(1.0.4) it's hardcoded 2 decimal places.

custom filter:

mymodule.filter('nofractioncurrency', [ '$filter', '$locale', function(filter, locale) { var currencyfilter = filter('currency'); var formats = locale.number_formats; homecoming function(amount, currencysymbol) { var value = currencyfilter(amount, currencysymbol); var sep = value.indexof(formats.decimal_sep); if(amount >= 0) { homecoming value.substring(0, sep); } homecoming value.substring(0, sep) + ')'; }; } ]);

template:

<div>{{price | nofractioncurrency}}</div>

example:

demo

update: fixed bug when handling negative values

angularjs filter formatting angularjs-filter

No comments:

Post a Comment