Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ngx-currency

nbfontana210kMIT19.0.0TypeScript support: included

A very simple currency mask directive that allows using a number attribute with the ngModel.

angular, currency, mask, model, money, ngx, number

readme

ngx-currency

npm version GitHub issues GitHub stars GitHub license

Demo

https://nbfontana.github.io/ngx-currency/

Table of contents

Getting Started

Installing and Importing

Install the package by command:

npm install ngx-currency --save

Import the directive

import { NgxCurrencyDirective } from "ngx-currency";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  imports: [NgxCurrencyDirective],
})
export class AppComponent {}

Using

<input type="text" inputmode="decimal" currencyMask formControlName="value" />
  • ngModel An attribute of type number. If is displayed '$ 25.63', the attribute will be '25.63'.

Options

You can set options...

<!-- example for pt-BR money -->
<input [currencyMask]="{ prefix: 'R$ ', thousands: '.', decimal: ',' }" formControlName="value" />

Available options:

  • align - Text alignment in input. (default: right)
  • allowNegative - If true can input negative values. (default: true)
  • decimal - Separator of decimals (default: '.')
  • precision - Number of decimal places (default: 2)
  • prefix - Money prefix (default: '$ ')
  • suffix - Money suffix (default: '')
  • thousands - Separator of thousands (default: ',')
  • nullable - when true, the value of the clean field will be null, when false the value will be 0
  • min - The minimum value (default: undefined)
  • max - The maximum value (default: undefined)
  • inputMode - Determines how to handle numbers as the user types them (default: Financial)

Input Modes:

  • Financial - Numbers start at the highest precision decimal. Typing a number shifts numbers left. The decimal character is ignored. Most cash registers work this way. For example:
    • Typing '12' results in '0.12'
    • Typing '1234' results in '12.34'
    • Typing '1.234' results in '12.34'
  • Natural - Numbers start to the left of the decimal. Typing a number to the left of the decimal shifts numbers left; typing to the right of the decimal replaces the next number. Most text inputs and spreadsheets work this way. For example:
    • Typing '1234' results in '1234'
    • Typing '1.234' results in '1.23'
    • Typing '12.34' results in '12.34'
    • Typing '123.4' results in '123.40'

You can also set options globally...

import { provideEnvironmentNgxCurrency, NgxCurrencyInputMode } from 'ngx-currency';

bootstrapApplication(AppComponent, {
  providers: [
    ...
    provideEnvironmentNgxCurrency({
      align: "right",
      allowNegative: true,
      allowZero: true,
      decimal: ",",
      precision: 2,
      prefix: "R$ ",
      suffix: "",
      thousands: ".",
      nullable: true,
      min: null,
      max: null,
      inputMode: NgxCurrencyInputMode.Financial,
    }),
    ...
  ],
}).catch((err) => console.error(err));

Development

Prepare your environment

  • Install Node.js and NPM
  • Install local dev dependencies: npm install while current directory is this repo

Development server

To start a local development server, run:

npm start

Building

To build the library run:

npm run build:lib

Testing

To execute unit tests with the Karma test runner, use the following command:

npm test

When running in the Chrome browser, you can set code breakpoints to debug tests using these instructions:

  • From the main Karma browser page, click the Debug button to open the debug window
  • Press ctrl + shift + i to open Chrome developer tools
  • Press ctrl + p to search for a file to debug
  • Enter a file name like input.handler.ts and click the file
  • Within the file, click on a row number to set a breakpoint
  • Refresh the browser window to re-run tests and stop on the breakpoint

License

MIT @ Neri Bez Fontana

changelog

Changelog

All notable changes to this project will be documented in this file. See commit-and-tag-version for commit guidelines.

19.0.0 (2025-01-06)

Features

Bug Fixes

  • allow work outside of forms (ffcc5f3)
  • create InputHandler in constructor (#195) (79161be)

18.0.0 (2024-06-04)

Features

  • allow currencyMask to be used as input instead of options (daf0c06)
  • migrate to Angular 18 (a11fdc3)

Bug Fixes

17.0.0 (2024-01-11)

⚠ BREAKING CHANGES

  • Migrate to Angular 17
  • migration to standalone components
  • more predictable directive and config names feat: partial global config override allowed refactor: stronger typing refactor: using the standard Angular project structure

build

  • Angular 17 + Update .nvmrc (3e01abe)

Features

Bug Fixes

  • allow null for min and max (5368031)
  • swap actual and expected values in config tests (745ee6a)

4.0.0 (2023-07-14)

⚠ BREAKING CHANGES

  • migration to standalone components
  • more predictable directive and config names feat: partial global config override allowed refactor: stronger typing refactor: using the standard Angular project structure

Features

Bug Fixes

  • allow null for min and max (5368031)
  • swap actual and expected values in config tests (745ee6a)

3.0.0 (2022-11-30)

Bug Fixes

  • allow negative at start of entry (dad7b33)

3.0.0 (2022-11-30)

Bug Fixes

  • allow negative at start of entry (dad7b33)

3.0.0 (2022-11-30)

Bug Fixes

  • allow negative at start of entry (dad7b33)

3.0.0 (2022-11-30)

Bug Fixes

  • allow negative at start of entry (dad7b33)

2.5.3 (2021-09-20)

Bug Fixes

  • Account for negative numbers (5542dc6)
  • allow backspacing of single whole numbers in Natural mode prior to the decimal (9530c9c)
  • export configuration token (be16f02)
  • stack overflow issue for precision > 2 (364644d)

2.5.2 (2020-11-10)

Bug Fixes

  • Account for negative numbers (5542dc6)
  • allow backspacing of single whole numbers in Natural mode prior to the decimal (9530c9c)

2.5.1 (2020-08-05)

Bug Fixes

  • ModuleWithProviders: necessary add type (fe6f64f)

2.5.0 (2020-08-05)

Bug Fixes

  • ModuleWithProviders: necessary add type (fe6f64f)

2.5.0 (2020-08-05)

Bug Fixes

  • ModuleWithProviders: necessary add type (fe6f64f)

2.5.0 (2020-08-05)

Bug Fixes

  • ModuleWithProviders: necessary add type (fe6f64f)

2.5.0 (2020-08-05)

Bug Fixes

  • ModuleWithProviders: necessary add type (fe6f64f)

2.4.1 (2020-08-05)

Bug Fixes

Build System

  • deps-dev: bump codecov from 3.6.5 to 3.7.1 (64a2cf1)
  • deps-dev: bump standard-version from 6.0.1 to 8.0.1 (1041bc0)

2.4.0 (2020-08-05)

Bug Fixes

Build System

  • deps-dev: bump codecov from 3.6.5 to 3.7.1 (64a2cf1)
  • deps-dev: bump standard-version from 6.0.1 to 8.0.1 (1041bc0)

2.3.3 (2020-04-22)

Bug Fixes

  • improve deletion in natural mode (2dce31e), closes #85

2.3.2 (2020-04-17)

Bug Fixes

  • added the CurrencyMaskInputMode to the exports (95059e6)

2.3.1 (2020-04-15)

Bug Fixes

  • Build: Error on ngc build (83acabe)

2.3.0 (2020-04-15)

Bug Fixes

  • Ignore special characters when validating maxLength (0dec5e1)

Build System

  • deps-dev: bump codecov from 3.4.0 to 3.6.5 (3fcbd48)

Features

  • addNumber: Adds support for natural input mode (7a18ef6)
  • addNumber: Typing decimal or thousands advances cursor (975e782)

Tests

  • canInputMoreNumbers: Add unit test for maxLength validation (7bf990b)

2.2.4 (2020-04-15)

Bug Fixes

  • issue with drag and drop partial selections. (4f8044d)
  • issue with entering special characters. (b7b1c59)
  • readOnly issue (64e3d1d)

Build System

2.2.3 (2020-04-14)

Bug Fixes

  • Delete button not clearing the value entirely. (dfb7518)

2.2.2 (2020-01-24)

Features

  • applyMask: Adds support for min and max (6843124)

2.2.1 (2020-01-02)

Bug Fixes

  • remove copyfile script from release script (84ffbe9)

2.2.0 (2019-12-26)

Features

  • build: Install and setup ng-packagr (c718bae)
  • build: Remove unused NPM build scripts (bbf4d69)

2.0.2 (2019-11-26)

Bug Fixes

  • ignored align in global config (1af2dd1)

2.0.1 (2019-11-23)

Bug Fixes

  • applyMask: Apply mask was not considering precision (58fb5fd)

Tests

  • applyMask: Unit test to method applyMask (c3556b4)

1.5.2 (2019-03-27)

1.5.1 (2018-11-26)

Bug Fixes

  • README: Updated readme to use forRoot module configuration (48d8ed4)

1.5.0 (2018-09-11)

Bug Fixes

  • forRoot: Added forRoot to the module for proper default config (c46c1c1)

1.4.0 (2018-09-10)

Bug Fixes

  • backspace: Added functionality to move the cursor back when backspacing separators (d5b1273)
  • backspace: Added testing (900c67e)
  • backspace: Functionality for backspacing beginning number (edge case) (4e2128d)
  • lint (23b3d5d)

Features

  • suppoer persian and arabic number (e3f0ff6)
  • support persian and arabic number (28f720a)

1.3.0 (2018-09-10)

Bug Fixes

  • backspace: Added functionality to move the cursor back when backspacing separators (d5b1273)
  • backspace: Added testing (900c67e)
  • backspace: Functionality for backspacing beginning number (edge case) (4e2128d)
  • lint (23b3d5d)

Features

  • suppoer persian and arabic number (e3f0ff6)
  • support persian and arabic number (28f720a)

1.1.1 (2017-09-29)

Bug Fixes

Features

1.1.0 (2017-09-29)

Bug Fixes

  • Ctrl+A and arrow navigation in Firefox (af16b14)

Features