0.5.1 • Published 4 years ago

@mtkcs/written-number-currency v0.5.1

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

WrittenNumberCurrency

Installation

npm i @mtkcs/written-number-currency

Usage

Under the hood written-number-currency uses the written-number library to convert number to words.

const wnc = require('@mtkcs/written-number-currency');

// There are only 2 presets for the moment. You can create your own and submit them
// if you want.
// Import a preset that corresponds to your language-currency combination.
const frDinarPresets = require('@mtkcs/written-number-currency/lib/presets/fr-dinar');

// locales for your language. Check the written-number library for all available locales:
const fr = require('written-number/lib/i18n/fr.json');

// create an instance
const instance = wnc({lang: fr, ...frDinarPresets});

// If we want to get the string representation of 1234 Dinar and 23 millimes (1234.023)
// amount = 1234: amount in dollars (euros, dinars, ...)
// precision = 23: amount in cents (centimes, millimes, ...)
const result = instance({amount: 1234, precision: 23});

console.log(result);
// => mille deux cent trente-quatre dinars et vingt-trois millimes

WrittenNumberCurrency

The arguments are:

  • lang: Object|required. A written-number valid language object.
  • linker: String|optional. The word that links between the amount and the precision
  • amountFormatter: Function|required if amount is provided. Function that accepts an amount an returns the correct grammatical form of the base currency.
  • precisionFormatter: Function|required if precision is provided. Function that accepts an amount an returns the correct grammatical form of the small currency.

Presets

Presets are objects that should contain 3 keys: linker amountFormatter precisionFormatter

Example:
// presets/en-dollar.js
const preset = {
  linker: 'and',
  amountFormatter(amount) {
    if (amount === 1) return 'dollar';
    return 'dollars';
  },
  precisionFormatter(amount) {
    if (amount === 1) return 'cent';
    return 'cents';
  },
}

Instance returned by WrittenNumberCurrency

The arguments are:

  • amount: Positive Integer|required if precision was not provided. The base currency of your choice, ex: dollar, euro, dinar...
  • precision: Positive Integer|required if amount was not provided. The small currency of your choice, ex: cent, millime, centime...

Licence

MIT

0.5.0

4 years ago

0.5.1

4 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago