units-convert
v1.2.3
Published
Convert between quantities in different units
Downloads
7
Readme
units-convert
This tool is modified from convert-units, adding the following functions:
- Solve the problem of floating-point precision after unit conversion
- More types of units
- Add 中文文档
A handy utility for converting between quantities in different units.
Installation
npm install units-convert --save
Usage
units-convert
has a simple chained API that is easy to read.
Here's how you move between the metric units for volume:
var convert = require('units-convert')
convert(1).from('l').to('ml')
// 1000
Jump from imperial to metric units the same way:
convert(1).from('lb').to('kg')
// 0.4536... (tested to 4 significant figures)
Just be careful not to ask for an impossible conversion:
convert(1).from('oz').to('fl-oz')
// throws -- you can't go from mass to volume!
You can ask units-convert
to select the best unit for you. You can also optionally explicitly exclude orders of magnitude or specify a cut off number for selecting the best representation.
convert(12000).from('mm').toBest()
// 12 Meters (the smallest unit with a value above 1)
convert(12000).from('mm').toBest({ exclude: ['m'] })
// 1200 Centimeters (the smallest unit excluding meters)
convert(900).from('mm').toBest({ cutOffNumber: 10 });
// 900 Centimeters (the smallest unit with a value equal to or above 10)
convert(1000).from('mm').toBest({ cutOffNumber: 10 })
// 10 Meters (the smallest unit with a value equal to or above 10)
You can get a list of the measurement types supported with .measures
convert().measures()
// [ 'length', 'mass', 'volume' ]
If you ever want to know the possible conversions for a unit, just use .possibilities
convert().from('l').possibilities()
// [ 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal' ]
convert().from('kg').possibilities()
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ]
You can also get the possible conversions for a measure:
convert().possibilities('mass')
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't' ]
You can also get the all the available units:
convert().possibilities()
// [ 'mm', 'cm', 'm', 'in', 'ft-us', 'ft', 'mi', 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't', 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal', 'ea', 'dz' ];
To get a detailed description of a unit, use describe
convert().describe('kg')
/*
{
abbr: 'kg',
measure: 'mass',
system: 'metric',
singular: 'Kilogram',
plural: 'Kilograms',
}
*/
To get detailed descriptions of all units, use list
.
convert().list()
/*
[{
abbr: 'kg',
measure: 'mass',
system: 'metric',
singular: 'Kilogram',
plural: 'Kilograms',
}, ...]
*/
You can also get detailed descriptions of all units for a measure:
convert().list('mass')
/*
[{
abbr: 'kg',
measure: 'mass',
system: 'metric',
singular: 'Kilogram',
plural: 'Kilograms',
}, ...]
*/
Supported Units
nm
: nanometerμm
: micrometermm
: millimetercm
: centimeterm
: meterkm
: kilometerin
: inchyd
: yardft-us
: U.S. survey footft
: footfathom
: fathommi
: milenMi
: nautical mile
mm2
: square millimetercm2
: square centimeterm2
: square meterha
: hectarekm2
: square kilometerin2
: square inchft2
: square footac
: acremi2
: square mile
mcg
: microgrammg
: milligramg
: gramkg
: kilogramoz
: ouncelb
: poundmt
: metric tont
: ton
mm3
: cubic millimetercm3
: cubic centimeterml
: milliliterl
: literkl
: kiloliterMl
: megaliterGl
: gigaliterm3
: cubic meterkm3
: cubic kilometertsp
: teaspoonTbs
: tablespoonin3
: cubic inchfl-oz
: fluid ouncecup
: cuppnt
: pintqt
: quartgal
: gallonft3
: cubic footyd3
: cubic yard
mm3/s
: cubic millimeter per secondcm3/s
: cubic centimeter per secondml/s
: milliliter per secondcl/s
: centiliter per seconddl/s
: deciliter per secondl/s
: liter per secondl/min
: liter per minutel/h
: liter per hourkl/s
: kiloliter per secondkl/min
: kiloliter per minutekl/h
: kiloliter per hourm3/s
: cubic meter per secondm3/min
: cubic meter per minutem3/h
: cubic meter per hourkm3/s
: cubic kilometer per secondtsp/s
: teaspoon per secondTbs/s
: tablespoon per secondin3/s
: cubic inch per secondin3/min
: cubic inch per minutein3/h
: cubic inch per hourfl-oz/s
: fluid ounce per secondfl-oz/min
: fluid ounce per minutefl-oz/h
: fluid ounce per hourcup/s
: cup per secondpnt/s
: pint per secondpnt/min
: pint per minutepnt/h
: pint per hourqt/s
: quart per secondgal/s
: gallon per secondgal/min
: gallon per minutegal/h
: gallon per hourft3/s
: cubic foot per secondft3/min
: cubic foot per minuteft3/h
: cubic foot per houryd3/s
: cubic yard per secondyd3/min
: cubic yard per minuteyd3/h
: cubic yard per hour
C
: CelsiusF
: FahrenheitK
: KelvinR
: Rankine
ns
: nanosecondμs
: microsecondms
: milliseconds
: secondmin
: minuteh
: hourd
: dayweek
: weekmonth
: monthyear
: year
Hz
: hertzmHz
: millihertzkHz
: kilohertzMHz
: megahertzGHz
: gigahertzTHz
: terahertzrpm
: revolutions per minutedeg/s
: degrees per secondrad/s
: radians per second
m/s
: meters per secondkm/h
: kilometers per hourmph
: miles per hourknot
: knotsft/s
: feet per secondin/h
: inches per hourmm/h
: millimeters per hour
s/m
: seconds per metermin/km
: minutes per kilometers/ft
: seconds per footmin/mi
: minutes per mile
Pa
: PascalhPa
: hectopascalkPa
: kilopascalMPa
: megapascalbar
: bartorr
: torrmH2O
: meter of water columnmmHg
: millimeters of mercurypsi
: pound per square inchksi
: kilo pound per square inch
b
: bitKb
: kilobitMb
: megabitGb
: gigabitTb
: terabitB
: byteKB
: kilobyteMB
: megabyteGB
: gigabyteTB
: terabyte
lx
: luxft-cd
: foot-candle
ppm
: parts per millionppb
: parts per billionppt
: parts per trillionppq
: parts per quadrillion
V
: VoltmV
: millivoltkV
: kilovolt
A
: AmperemA
: milliamperekA
: kiloampere
W
: WattmW
: milliwattkW
: kilowattMW
: megawattGW
: gigawattPS
: petawattBtu/s
: British thermal unit per secondft-lb/s
: foot-pound per secondhp
: horsepower
VA
: volt-amperemVA
: millivolt-amperekVA
: kilovolt-ampereMVA
: megavolt-ampereGVA
: gigavolt-ampere
VAR
: volt-ampere reactivemVAR
: millivolt-ampere reactivekVAR
: kilovolt-ampere reactiveMVAR
: megavolt-ampere reactiveGVAR
: gigavolt-ampere reactive
Ws
: watt-secondWh
: watt-hourmWh
: milliwatt-hourkWh
: kilowatt-hourMWh
: megawatt-hourGWh
: gigawatt-hourJ
: joulekJ
: kilojouleMJ
: megajouleGJ
: gigajoule
VARh
: volt-ampere reactive hourmVARh
: millivolt-ampere reactive hourkVARh
: kilovolt-ampere reactive hourMVARh
: megavolt-ampere reactive hourGVARh
: gigavolt-ampere reactive hour
deg
: degreerad
: radiangrad
: gradianarcmin
: arcminutearcsec
: arcsecond
c
: coulombmC
: milliampereμC
: microcoulombnC
: nanocoulombpC
: picocoulomb
N
: NewtonkN
: kilonewtonlbf
: pound-force
g (g-force)
: acceleration due to gravitym/s2
: standard acceleration
pcs
: piecesbk-doz
: baker's dozencp
: centsdoz-doz
: dozen dozendoz
: dozengr-gr
: gross grossgros
: grosshalf-dozen
: half a dozenlong-hundred
: long hundredream
: reamscores
: scoressm-gr
: small grosstrio
: trio