Giter Site home page Giter Site logo

izolate / millify Goto Github PK

View Code? Open in Web Editor NEW
177.0 2.0 13.0 1012 KB

Convert long numbers to pretty, human-readable strings

Home Page: https://npmjs.com/package/millify

License: MIT License

TypeScript 53.86% JavaScript 46.14%
javascript units pretty human-readable human-readable-units abbreviations

millify's Introduction

Millify

Converts long numbers into pretty, human-readable strings.

Before ๐Ÿ˜’ After ๐ŸŽ‰
2000 '2K'
10000 '10k'
42500 '42.5 kg'
1250000 '1.25 MB'
2700000000 '2.7 bil'

Install

Get it on npm:

npm install millify

Usage

Command line

$ millify 12345
12.3K

See millify --help for options.

Programmatically

millify(value: number, options: MillifyOptions)

import millify from "millify";

// For CommonJS: `const { millify } = require("millify");`

millify(2500); // 2.5K

millify(1024000, {
  precision: 3,
  lowercase: true
});
// 1.024m

millify(39500, {
  precision: 2,  
  locales: "de-DE"
});
// 3,95K

millify(1440000, {
  units: ["B", "KB", "MB", "GB", "TB"],
  space: true,
});
// 1.44 MB

Options

Name Type Default Description
precision number 1 Number of decimal places to use
locales string | Array<string> browser language Formats the number in different languages
lowercase boolean false Use lowercase abbreviations
space boolean false Add a space between number and abbreviation
units Array<string> ['', 'K', 'M', 'B', 'T', 'P', 'E'] Unit abbreviations

millify's People

Contributors

amio avatar dbankier avatar izolate avatar kikobeats avatar maxprilutskiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

millify's Issues

significant digits?

I would like to specify a maximum number of significant digits for a number, so that I may better estimate how much space to allocate in design. Currently I may only specify precision and control decimals.

Currently:

  • millify(123456, {precision: 2}) returns 123.45K

Proposed:

  • millify(123456, {digits: 3}) returns 123K
  • millify(1234, {digits: 3}) returns 1.23K.

I'm willing to work on a patch if this is in scope for millify.

RFE: Should support strict precision

The precision argument isn't always honored if the resulting string ends in zeroes. There should be some kind of "padding" or "strict precision" argument so that the number is padded with trailing zeroes in the result. This would allow for right-aligned text (e.g. in tables) to always line up correctly.

Any plans to support BigInts?

Hi there!
I noticed we throw an error whenever we're out of the range of Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER. Are there any plans to support BigInt so we can exceed this range sometime in the future?
Cheers

`millify` does not check for `undefined` or `null` values

When first calling millify the package passes the value to parseValue. On the first line there it calls value.toString() without first checking if value actually exists:

millify/lib/utils.ts

Lines 4 to 14 in 8cc295d

export function parseValue(value: number): number {
const val: number = parseFloat(value.toString());
if (isNaN(val)) {
throw new Error(`Input value is not a number`);
}
if (val > Number.MAX_SAFE_INTEGER || val < Number.MIN_SAFE_INTEGER) {
throw new RangeError("Input value is outside of safe integer range");
}
return val;
}

// Change this
const val: number = parseFloat(value.toString());

// To this
const val: number = parseFloat(value?.toString());

See this StackOverflow post for an example of how this can be problematic

millify is not a function

I'm using this in a discord bot.
My code is below

const millify = require("millify");

    const views = millify(npsong.views);
    const like = millify(npsong.likes);
    const lislikes = millify(npsong.dislikes);

I'm getting the error millify is not a function

Types don't match code when this library is used in ESM

This library is CJS, so when used from an ESM module, you have to do this:

import millify from 'millify';

millify.default(...)

However, this doesn't match up with the published types, which expect the default export to be used directly.

Throw error if unit index is greater than units array size

If the unitIndex is greater than the size of the units array, we currently choose the last element in the array as a fallback.

This is WRONG and leads to incorrect results. Throw an error instead to notify the user what needs to change (more elements in the units array).

Not working with some numbers

I have found a few numbers that are not being abbreviated:
1000000000000000000
210000000000000000
105000000000000000

I am having this issue on Brave browser, if that is of any help.

Values rounded up to 1000K are not converted to 1M

Values which are supposed to be rounded up to 1M are being shown as 1000K instead. This needs to be fixed otherwise its an edge case that consumers of this module need to work around.

See screenshot below from RunJS (a JS sandbox).

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.