1.0.1 • Published 5 years ago

friendly-numbers v1.0.1

Weekly downloads
149
License
MIT
Repository
github
Last release
5 years ago

friendly-numbers

JS library to manage and format numbers with Thousand (k), Million (M), etc. syntax and also to format small decimal numbers.

Introduction

Zero dependency library to fromat numbers with short format. For example 15430 => 15K or 15430 => 15.4K.

Another config proerty will help to manage decimals for very small numbers, where you can select how many decimals numbers you want to show after all 0s. For example 0.0000345466 => 0.000034.

Installation

with npm

$ npm install friendly-numbers

with yarn

$ yarn add friendly-numbers

Usage

format

This function will shrink the received number if it is bigger than 999 and it will returned a string with the TBMK corresponding notation.

import { format } from 'friendly-numbers';

...

const totalBalance = 5000;
const formattedBalance = format(totalBalance);
// result stored in formattedBalance will be '5K'

...
UnitsRangeExampleOutput
Units, Tens, Hundreds(0 - 999)500500
Thousands(1000 - 999999)50005K
Millions(1000000 - 999999999)50000005M
Billions(1000000000 - 99999999999999)50000000005B
Trillions(1000000000000 - 99999999999999999)50000000000005T

Optional Config

There is an optional config parameter that can be passed to format function

const config = {
  // Define decimal numbers to display in the decimal part, if decimals present
  decimals: 2,
  // Number of decimals to display (if required) for formatted numbers
  // e.g. 0 decimals: 4123 => 4K | 1 decimal: 4123 => 4.1K | 2 decimals: 4123 => 4.12K | ...
  formattedDecimals: 2,
  // Override previous rule in case of 0.00XXX in case of more than X amounts of 0
  // in order to display something more meaningful than a simple 0
  smallMinimumMeaningfulDigits: 2
};

...

const formattedNumber = format(number, config);
ParameterDefault ValueDescriptionExample
decimals2Decimal numbers to display in the decimal part, if required. In case of extra decimals beyond the defined number, the number will be truncated and rounded.- number: 0.123456 - decimals: 2 => 0.12 - decimals: 4 => 0.1234 - number: 5.2000 - decimals: 2 => 5.20 - decimals: 4 => 5.2000- number: 7.0000 - decimals: 2 => 7 - decimals: 4 => 7
formattedDecimals0If this property is set (> 0) it will add N decimals to numbers formatted with notation TBMK, if required. In case of extra numbers beyond the defined number, the number will be truncated and rounded. number: 4123 - formattedDecimals: 0 => 4K - formattedDecimals: 1 => 4.1K - formattedDecimals: 2 => 4.12K number: 2876 - formattedDecimals: 0 => 3K - formattedDecimals: 1 => 2.9K - formattedDecimals: 2 => 2.88K
smallMinimumMeaningfulDigits0If this property is set (> 0), in case of numbers of 0.00..0XXXX it will display as many decimals required until the first non-zero decimal and from this point until N number defined. In the case of extra decimals beyond the defined number, the number will be truncated and rounded. number: 0.001234 - smallMinimumMeaningfulDigits: 0 => 0.00 - smallMinimumMeaningfulDigits: 1 => 0.001 number: 0.001876 - smallMinimumMeaningfulDigits: 0 => 0.00 - smallMinimumMeaningfulDigits: 1 => 0.002 - smallMinimumMeaningfulDigits: 2 => 0.0019

deFormat

This function will parse formatted numbers with format function in the notation of TBMK into full numbers. Note Please bear in mind that the usage of this function it is not recommended as it can produce a loss of precission. The recommended approach is to keep in the code the full number and only formatted during the render process.

import { deFormat } from 'friendly-numbers';

...

const formattedBalance = "5K";
const balance = deFormat(formattedBalance);
// result stored in balance will be 5000

...

Precission loss example:

import { format, deFormat } from 'friendly-numbers';

...
const originalBalance = 5555;
const formattedBalance = format(originalBalance);
const balance = deFormat(formattedBalance);
// result stored in formattedBalance will be "5k"
// result stored in balance will be 5000

...

Contributions

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

The MIT License (MIT)

Copyright (c) 2019 Alvaro Jimenez Martin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.