numeros
v1.0.4
Published
small javascrip utility to format numbers in a short and standerd method including (currencies,timing,computer storage unit sizes, Metrics Systems)
Downloads
6
Maintainers
Readme
Introduction
numeros is a lightweight, small, simple javascript liberary that helps you to formate your numbers into different forms including Currency, Timing, Computer Storage Units and Metric System Units (gram, liter, meter)
Installation
npm install numeros@latest
Usage
Currency Format
if you have any number that specify an amount of a specific currency, use this class as following
// in case you are not using latest es6 import syntax
const { CurrencyFormatter } = require("numeros");
// in case you are latest es6 syntax
import { CurrencyFormatter } from "numeros";
// create an instance of the class
const cf = new CurrencyFormatter();
in short format
the short formate method take an amount in an currency (default is USD) and return it in a shorten way like this
// value bigger then 1 thousend dollar by default
console.log(cf.shortFormat(2000)); // USD2.0K
// value can be negative however the result will be the same
console.log(cf.shortFormat(-5250360.6)); // USD5,3M
// value can be between Trillion and few cents of a currency
console.log(cf.shortFormat(2 * 10 ** 12)); // USD2,0T
console.log(cf.shortFormat(0.35)); // USD0.53
// you can add the currency manually like this
console.log(cf.shortFormat(3500, "EUR")); // EUR3,5K
// or use the currency sign like this
// supported signes are (USD,EUR, GBP, YUA, RUB, INR, YEN)
console.log(cf.shortFormat(48350, cf.currenciesList.EUR)); // £48,4K
// you can change the seperator like this
// you can use (.)(_)( )(:) anything u like
// default will be ','
console.log(cf.shortFormat(500000), cf.currenciesList.EUR, "."); // EUR500.0K
In standard format
standard format method has the same options as short format , however the value will remain the same in standard format without any roundings as so:
console.log(cf.standerdFormat(1800320, "GBP")); // GBP 1,800,320.00
// in case was any cents
console.log(cf.standerdFormat(2500.32)); // USD 2,500.32
// you can change the seprator like so
console.log(cf.standerdFormat(88356.36), "EUR", "."); // EUR 88.356.36
console.log(cf.standerdFormat(88356.36), "EUR", "_"); // EUR 88_356.36
Timing Format
you will basicly use this formatter when dealing with times in milliseconds coming from a database the value will be converted to seconds, mintues, hours... and other timing units until year
const { TimingFormatter } = require("numeros");
// create an instance
const tf = new TimingFormatter();
// the value base will be in millisecond like so
console.log(tf.formate(3000)); // 3 seconds
// minutes
console.log(tf.formate(35 * 1000 * 3600)); // 35minutes
// days
console.log(tf.formate(2 * 1000 * 3600 * 24)); // 2days
Computer Storage Unit Format
use this to convert between the differnet storage unit of the computer and format the number like so:
const { ComputerStorageUnitFormatter } = require("numeros");
// create an instance
const csuf = new ComputerStorageUnitFormatter();
// the base unit is the bit
console.log(csuf.formate(5)); // 5b => 5bits
// you can convert to other units by increasing the number of bits
console.log(csuf.formate(1024)); // 1024 bit => 128.0Byte
// in MB
console.log(csuf.formate(8388608)); // 8388608 bit => 8.0MB
// in GB
console.log(csuf.formate(10737418240)); // 10737418240 bit => 10.0GB
// you can change the seprator
console.log(csuf.formate(10737418240, ":")); // 10:0GB
console.log(csuf.formate(10737418240, "_")); // 10_0GB
console.log(csuf.formate(10737418240, "*")); // 10*0GB
Metric System Format
as the name suggeset convert between the metric system units (milli, centi, deci, deca, hecto, kilo) of the base unit gram for mass and meter for distance and liter for volume
const { MetricSystemUnit } = require("numeros");
// create an instance
const msu = new MetricSystemUnit();
// for mass(gram) by default or other units
// it will be same thing only the basic unit
console.log(msu.formate(15, "gram")); // 1.5deg (decagram)
console.log(msu.formate(22500.32, "gram")); // 22kg (kilogram)
console.log(msu.formate(0.25, "gram")); // 2dg (decigram)