npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cstan/number-formatter

v1.0.3

Published

Can format number with prefix and/or suffix and optionally markers at thousands places.

Downloads

6

Readme

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 => ","