Skip to content

abranhe/short-numbers

Repository files navigation

short-numbers Build Status

Make short numbers from long numbers

Why?

  • Supports space before sufix.
  • Include type definitions.

Install

$ npm install short-numbers

Usage

const shortNumbers = require('short-numbers');

shortNumbers('1000')
// => '1K'

shortNumbers(1000)
// => '1K'

shortNumbers('10300')
// => '10.3K'

shortNumbers('1000000')
// => '1M'

shortNumbers('1000000000')
// => '1B'

shortNumbers('1000000000000')
// => '1T'

shortNumbers('10300', { space: true })
// => '10.3 K'

shortNumbers('1000000', { m: 'million', space: true })
// => '1 million'

API

shortNumbers(input, [options])

input

Type: string | number

The input number you'd like to convert to a short number. It must be a parsable to number string or a number.

options

Type: object

k

Type: string
Default: 'K'

Set the value of the k which is the prefix 'kilo'.

m

Type: string
Default: 'M'

Set the value of the m which is the prefix 'million'.

b

Type: string
Default: 'B'

Set the value of the b which is the prefix 'billion'.

t

Type: string
Default: 'T'

Set the value of the t which is the prefix 'trillion'.

space

Type: string
Default: true

Specify if the contains space before sufix.

Todo

  • Supports for plural sufixes.

Current behavior

shortNumbers('2000000', { m: 'million like', space: true })
// => 2 million like

Desired behavior

// => 2 million likes
  • Suport for prefix. An optional string to prepend to the value, e.g. '$'.

License

MIT © Abraham Hernandez