Ember CLI Addon for Accounting.js

2.1.0 · active · verified Wed Apr 22

Ember-cli-accounting is an Ember CLI addon that integrates the `accounting.js` library into Ember applications, providing robust number and currency formatting capabilities. It ports `accounting.js` (specifically version 0.4.1 as of addon version 2.1.0) to ES6 modules, allowing for selective imports to optimize bundle size. The addon ships with both JavaScript functions and convenient Handlebars helpers (`format-number`, `format-money`) for use directly in templates. It also supports configuring default formatting options via Ember initializers. The addon's versioning does not match the underlying `accounting.js` library, but the maintainer aims to keep it updated with the original library's bug fixes and features. The current stable version is 2.1.1, with recent updates addressing Ember deprecations. It is distinct from directly including `accounting.js` by offering deep Ember integration and ES module support.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing specific accounting functions and settings, configuring global defaults via an initializer, and using both JavaScript functions and Handlebars helpers for formatting currency and numbers.

import formatMoney from 'accounting/format-money';
import formatNumber from 'accounting/format-number';

// app/initializers/accounting-defaults.js
import { currency, number } from 'accounting/settings';

export default {
  name: 'accounting-defaults',
  initialize() {
    currency.symbol = '€';
    currency.format = '%v %s'; // e.g., '123.45 €'
    number.decimal = ',';
    number.thousand = '.';
    number.precision = 2;
  }
};

// app/components/my-component.js
import Component from '@ember/component';
import { computed } from '@ember/object';

export default Component.extend({
  price: 12345.67,
  quantity: 5,

  formattedPrice: computed('price', function() {
    return formatMoney(this.price, { symbol: '$', precision: 2 }); // Uses local options
  }),

  // In a Handlebars template (app/templates/components/my-component.hbs):
  // <h1>Product Details</h1>
  // <p>Price: {{format-money price}}</p>  <!-- Uses initializer defaults: '12.345,67 €' -->
  // <p>Quantity: {{format-number quantity}}</p> <!-- Uses initializer defaults: '5' -->
  // <p>Discounted Price: {{format-money 99.99 symbol='USD' format='%s %v'}}</p> <!-- Overrides for specific use -->
});

view raw JSON →