星期四, 10月 16, 2014

[AngularJS] currency 去除小數點decimal-cents

今天用到Angular預設的currency filter時,發現都會在小數點後多二個 00,
查了一下可以透過自定的currency來解決,請參考以下這篇討論串:

http://stackoverflow.com/questions/14782439/removing-angularjs-currency-filter-decimal-cents




新增noFractionCurrency

var myAppFilter = angular.module('myApp.filters',[]);


myAppFilter.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);

      console.log(currencySymbol);
      if(amount >= 0) {
        return value.substring(0, sep);
      }
      return value.substring(0, sep) + ')';
    };
} ]);

使用方法

{{money| noFractionCurrency :''}}//第一個參數為單位字串

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails