
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
number-display
Advanced tools
Display number smartly within a certain length.
result = display(input, length, config)
To display data in a width-limited component, this function will smartly help you to convert number to a certain chart length. To be simple, plain, flexible and accurate, the conversion follow this rules:
yarn add number-display
or
npm i number-display --save
import display from 'number-display';
// result = display(input, length, config)
// config: {maxAccuracy, placeholder, allowText}
display(1234.123) // 1,234.12
display(1234.123, 4) // 1.2k
display('1000000000') // 1G
display(-1.2345e+5) // -123,450
display(NaN) // --
display('') // --
display(new Date()) // --
display((new Date()).toISOString()) // 2018-09-
display(NaN, 2, {placeholder: '**'}) // **
display(1.22, 8, {maxAccuracy: 0}) // 1.2
display('text', 8, {allowText: false}) // --
display(12345678, 19, {comma: false}) // 12345678
display(1234567867, 8, {unitsMaxAccuracy: 2}) // 1.23G
display(12345, 4, {units: ['t', 'h', 'k', 'tk'], unitsInterval: 1}) // 1tk
display(123457777, 4, {units: ['t', 'h', 'k', 'tk'], unitsInterval: 1}) // --
input
The thing you want to display as a number. When it can't be converted to a number, the result would be a placeholder.
length
( default: 8 )
The max length the result would be. length should no less then 4 so that any number can display ( say -123 ) after trim.
config
maxAccuracy
( default: 4 )
The max accuracy. The final accuracy will be calculated by length(of course less than maxAccuracy), so usually you don't need to set this param.
placeholder
( default: '--' )
The result when the input can't be converted to a number. it's length should no more than your length param.
allowText
( default: true )
Allow Text ( String that cant convert to number/null/undefined/NaN ) as input and result. If false , result of text will be placeholder.
comma
( default: true )
Whether the locale string has commas ( 1,234,222 ).
units
(defult: ['k', 'M', 'G', 'T'])
Units for result number after compressed, start with lowest one, which is for 1*unitInterval compression. If number is greater than the highest unit can display , it will return placeholder.
unitInterval
(defult: 3)
The interval figures between each units. If units is defult ['k', 'M', 'G', 'T'], unitInterval should be defult 3.
unitMaxAccuracy
(defult: 4)
The max accuracy when displayed with units.
FAQs
Display number smartly within a certain length.
The npm package number-display receives a total of 23 weekly downloads. As such, number-display popularity was classified as not popular.
We found that number-display demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.