1.0.3 • Published 1 year ago

@cstan/number-formatter v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

number-formatter

A CommonJS module for formatting number for printing or display.

This is a very versatile number formatter that can be used quite easily in other apps such as jspdf. The options available include:

  • A string prefix such as "$ " or "Labor: $ "
  • A choice of decimal point or decimal comma
  • Display of thousands separator optional
  • A choice of negative number display -1,234.56 OR (1,234.56)
  • Set a maximum length of the whole number portion of the number
  • Set a minimum length of the whole number portion of the number
  • Set the precision (number of places) of the fraction portion of the number
  • A string suffix such as " liters" or " /hour" (Leading spaces are optional)

These can be set when the object is instantiated by entering the configuration object as an argument to the constructor OR you can call the configure() method of the object to configure or reconfigure the desired format. Below is the DEFAULT configuration.

{
  prefix: "", // default is empty string
  useDecimalPoint: true, // the default value uses a "." for the decimal marker, and a value of false
    // uses the decimal comma. The thousands separator, if used, is automatically switched.
  displayThousandsSep: false, // true causes the thousands separator to display.
  wholeMaxLen: 12, // this sets the maximum digits allowed in the whole number. It does not
    // include the decimal, the fraction part or the thousaqnds separators.
  wholeMinLen: 1, // the minimum digits of the whole portion. With default: 0 => "0.00"
  precision: 2, // the number of digits after the decimal. With default: .7 => "0.70" OR .7986 => "0.79"
  suffix: "", // default is empty string
  negUseParen: false, // default uses a hyphen "-" as a negative sign. If this is true then negative
    // numbers are enclosed by parenthesis "(" and ")".
}

Any combination or nothing may be used as an argument for configuring NumberFormatter. Examples below.

Installation:

npm install @cstan/number-formatter

Use

import { NumberFormatter } from "@cstan/number-formatter"

const dollars = new NumberFormatter({ prefix: "$ ", displayThousandsSep: true });
// Note: dollars is assigned the formatter

let due = 1234.56; // May be number or string
console.log(`Amount due: ${dollars.format(due)}`);
// Amount due: $ 1,234.56

OR

let due = 1234.56;
let dollars = {
  prefix: "$",
  displayThousandsSep: true,
};
Note: dollars is assigned the configuration object

const fmtr = new NumberFormatter(dollars);
console.log(`Amount due: ${fmtr.format(due)}`);
// Amount due: $1,234.56

let pounds = {
  prefix: "",
  suffix: " lbs."
};
let wt = 4567.89

fmtr.configure(pounds);
console.log(`Net weight: ${fmtr.format(wt)}`);
// Net weight: 4,567.89 lbs.

Special features allow @cstan/number-formatter to provide information about the formatted number to its parent for managing input into an HTML input element so that the display of the number within the input element is formatted. These special values are accessed as if they were public, read-only attributes of the object. The following examples demonstrate their use:

import { NumberFormatter } from "@cstan/number-formatter"

const dollars = new NumberFormatter({ prefix: "$ ", displayThousandsSep: true });
let due = 1234.56;
console.log(`Amount due: ${dollars.format(due)}`);
// Amount due: $ 1,234.56

let val = dollars.leftLimit; // val => 2

let val = dollars.decimalPos; // val => 7

let val = dollars.rightLimit; // val => 10

let val = dollars.precision; // val => 2

let val = dollars.hasKsep; // val => true

let val = dollars.isNegative; // val => false

let val = dollars.sepChar; // val => ","
1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago