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

ziffer

v3.0.0

Published

NodeJS simple number formatter

Downloads

109

Readme

Ziffer

Build Code Climate

NodeJS simple number formatter.

Install

npm i ziffer

Usage

const ziffer = require("ziffer");
const euro   = ziffer({ inprefix: "€ ", decimals: 2 });

// prints "€ 12 345,68"
console.log(euro.format(12345.678));

// prints "-€ 12_345::7"
console.log(euro.format(-12345.678, { decimals: 1, thousands: "_", decimal: "::" }));

// prints "(€ 12 345,68)"
console.log(euro.format(-12345.678, { negative: "paren" }));

// prints -12345.678
console.log(euro.unformat("(€ 12 345,68)", { negative: "paren" }));

Options

When creating a ziffer formatter you can pass any of these options in an optional object. These will be the defaults to subsequent .format() calls. You can also set an individual option in a specific call by passing a second options argument as seen above.

List of Options

  • decimal: separator between integer and fraction (default: comma)
  • thousands: separator between integer groups (default: space)
  • outprefix: string prefix (default: none)
  • inprefix: string prefix (default: none)
  • insuffix: string suffix (default: none)
  • outsuffix: string suffix (default: none)
  • negative: how negative values are expressed - "left", "right" or parenthesis (default: left)
  • group: size of digit groups separated by thousands (default: 3)
  • group_except: exception for digit grouping (default: 4)
  • decimals: number of decimals in fraction to round to (default: no rounding)
  • digits: a string with 10 digits from 0 to 9 (default: empty, which means 0-9)

The reason there's an in and out prefix and suffix is because of how negative values are formatted. The negative characters (minus or parenthesis) are placed in between, which leaves you the opportunity to format any way you like.

Here's a weird example:

const ziffer = require("ziffer");
const euro   = ziffer({
    outprefix : "<",
    inprefix  : "[",
    insuffix  : "]",
    outsuffix : ">",
    negative  : "paren"
});

// prints <([123])>
console.log(euro.format(-123));
// notice how parenthesis are place in between

Advanced

Group

Integer digit grouping can also be configured using a list of group sizes instead of a single number. This allows, for example, to group digits the way Hindi do.

const ziffer = require("ziffer");
const hindi  = ziffer({
    decimal   : ".",
    thousands : ",",
    group     : [ 3, 2 ],
    decimals  : 2
});

// prints 12,34,56,789.00
console.log(hindi.format(123456789));

Digits

For example, converting numbers to Arabic numeral can be done like the following:

const ziffer = require("ziffer");
const arab   = ziffer({
    digits : "٠١٢٣٤٥٦٧٨٩"
});

// prints ١٥٩
console.log(arab.format(159));