ember.js

Currency formatting template helper

Remarks#

More details available in Ember guides, where this example was taken from.

Compatible with Ember 2.2.0+ (2.11.0 was the latest at the time of writing)

Creating a new helper

Use Ember CLI to generate a new helper in your app:

ember generate helper format-currency

Then edit helpers/format-currency.js to contain the following:

import Ember from 'ember';

export function formatCurrency([value, ...rest]) {
  const dollars = Math.floor(value / 100);
  const cents = value % 100;
  const sign = '$';

  if (cents.toString().length === 1) { cents = '0' + cents; }
  return `${sign}${dollars}.${cents}`;
}

export default Ember.Helper.helper(formatCurrency);

Now you can use {{format-currency model.someNumericValue}} in templates.


A unit test for the new helper is automatically created in tests/unit/helpers/


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow