1.0.2 β€’ Published 4 years ago

@currency-formatter/price-formatter v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago
  • Works in all browsers
  • Accepts custom pattern
  • Heavily tested
  • No dependency
  • Supports ES modules
  • Supports AMD/CommonJS
  • Supports Modern/UMD module
  • Supports UMD module


✨ Features

  • One dependency to convert float value representing price by providing pattern.
  • Customize price by different patterns
  • Use same pattern to format the price throughout the application
  • Generic pattern with flexibility to use currency symbol/code, optional decimal

πŸ”§ Installation

1️⃣ Install by running: npm i --save @currency-formatter/price-formatter

πŸ“¦ Pattern

// Starts with Currency
{cCUR }{n-}#{t,}##{t,}###{t,}##{t,}##(t,}###{d.}##

// Ends with Currency
{n-}#{t,}##{t,}###{t,}##{t,}##(t,}###{d.}##{c CUR}

// Starts with Currency
// optional decimals separator means that in case the float does not contain any decimals, don't show decimals
{cCUR }{n-}#{t,}##{t,}###{t,}##{t,}##(t,}###{o.}##

Explanation of components in pattern:

  • β€œ{cCUR }” = currency code (e.g. β€œ{cEUR }” or β€œ{cUSD }”) or currency symbol (e.g. β€œ{c €}” or β€œ{c$ }”);
  • β€œ{n-}” = negative value indicator (β€œn”) and character(s) to be used to indicate negative value. Example: β€œ-” ; Note: the negative value indicator should only be used in case the float contains a negative value!
  • β€œ#########” = to be replaced by the Float value; Note: the number signs between β€œ{n?}” and β€œ{d?}” or β€œ{o?}” needs to be used for major part of the float (150 in case float contains 150.01) and the number signs after β€œ{d?}” or β€œ{o?}” needs to be used for minor part of the float (01 in case float contains 150.01)!
  • β€œ{t,}” = thousands separator (β€œt”) and character to be used to indicate the separator (β€œ,”). Examples: β€œ ” (space), β€œ,” (comma), β€œ.” (dot) or β€œβ€™β€ (apostrophe); Note: the pattern can contain multiple thousands separators!
  • β€œ{d.}” = decimals separator (β€œd”) and character to be used to indicate the separator (β€œ.”). Examples: β€œ,” (comma) or β€œ.” (dot); Note 1: the pattern can contain only one decimals separators! Note 2: in case β€œ{d?}” is being used and there are no decimals in the float, the number signs for the decimals needs to be replaced with zero’s (0)! Note 3: in case β€œ{d?}” is being used and there are less decimals in the float as there are available number signs for decimals in the pattern, the remaining number signs for the decimals needs to be replaced with zero’s (0)! Note 4: in case β€œ{d?}” is being used and there are more decimals in the float as there are available number signs for decimals in the pattern, the float needs to be rounded!
  • β€œ{o.}” = optional decimals separator (β€œo”) and character to be used to indicate the separator (β€œ.”). Examples: β€œ,” (comma) or β€œ.” (dot). The optional decimals separator means that in case the float does not contain any decimals, the decimals including the decimals separator should not be shown; Note 1: in case β€œ{o?}” is being used and there are less decimals in the float as there are available number signs for decimals in the pattern, the remaining number signs for the decimals needs to be replaced with zero’s (0)! Note 2: in case β€œ{o?}” is being used and there are more decimals in the float as there are available number signs for decimals in the pattern, the float needs to be rounded!

πŸ“¦ Usage

import PriceFormatter from './price-formatter.module.js';

const pattern = "{c€ }{n-}#{t.}###{t.}###{t.}###{o,}##",
	formatter = new PriceFormatter(pattern);
	formatter.format('9876543210.1234');

output: € 9.876.543.210,12

πŸ₯‚ License

MIT