Git Product home page Git Product logo

number-abbreviate's Introduction

Abbreviate Number

Abbreviate number to a more human-friendly format (3.6K, 6.8M, etc.)

MIT License Language PRs Welcome

build coverage Language grade: JavaScript NPM Package NPM Downloads

Install

npm install numabbr --save

Usage

Simple/shorthand mode:

import numabbr from 'numabbr'

numabbr(100)           // => 100
numabbr(10000)         // => 10K
numabbr(-10000)        // => -10K
numabbr(1000000)       // => 1M
numabbr(1020000)       // => 1.02M
numabbr(1000000000)    // => 1B
numabbr(1000000000000) // => 1T

numabbr(123456, { precision: 1 }) // => 123.5K

numabbr(123456, {
      precision: 1,
      commatize: { division: 2 },
}) // => 1,23.5K

Class/constructor mode:

import { NumberAbbreviate } from 'numabbr'
const abbr = new NumberAbbreviate({
  : 1000,
  : 10000,
  十万: 100000,
  百万: 1000000,
  千万: 10000000,
  亿: 100000000,
})

abbr.abbreviate(123)    // => 123
abbr.abbreviate(1000)   // => 1千
abbr.abbreviate(1234)   // => 1.23千
abbr.abbreviate(10000)  // => 1万
abbr.abbreviate(12345)  // => 1.23万
abbr.abbreviate(123456) // => 1.23十万

Options

  • precision { Number } Default 2. Specify the precision of decimal part
  • commatize { Object | Boolean } Default false. Specify how to commatize the result
    • division { Number } Default 3. Length of each divided parts
    • separator { Char } Default ,. Separator of each divided parts

Contributing

Pull requests and stars are highly welcome.

For bugs and feature requests, please create an issue.

License

The scripts and documentation in this project are released under the MIT License s

number-abbreviate's People

Contributors

bubkoo avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

alexrogalskiy

number-abbreviate's Issues

Support BigNumber Type

Using BigNumber.js

If i enter a number which is type BigNumber, i will be prompt an error. Are we considering to support BigNumber type?

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.