Polyconvert is a library of functions for converting between different units of measurement. It currently consists of 17 top level categories, each with several different units of measurement. The project currently supports JavaScript only but it would be nice to add more languages in the future!
If you find this tool useful you may also like this abbreviations gist and this formatting gist to help you turn your converted values into legible strings.
Table of Contents:
To include the Polyconvert library you have two options:
Package on npm: https://www.npmjs.com/package/@jgphilpott/polyconvert
Install with:
npm i @jgphilpott/polyconvert
Then require it:
const polyconvert = require('@jgphilpott/polyconvert')The other option you have is to link to the polyconvert.min.js file from an HTML document for use in the browser, like this:
<script src="polyconvert.min.js"></script>You can either link to the polyconvert.min.js file in this repo or download it and use your own copy.
Instructions on how to use the formulas:
The library features the polyconvert object that contains all of the formulas. The top level keys represent all the different categories available, see the object map below:
polyconvert = {
angle: {...},
area: {...},
data: {...},
density: {...},
dimension: {...},
energy: {...},
frequency: {...},
illumination: {...},
length: {...},
magnetomotive: {...},
mass: {...},
pressure: {...},
radiation: {...},
speed: {...},
temperature: {...},
time: {...},
volume: {...}
}The second level keys represent all the different units of measurement available within that category. See the object map below using the data category as an example:
polyconvert.data = {
bit: {...},
byte: {...},
kilobyte: {...},
megabyte: {...},
gigabyte: {...},
terabyte: {...},
petabyte: {...},
exabyte: {...},
zettabyte: {...},
yottabyte: {...}
}The third level keys represent all the different units of measurement that you can convert the original unit into and the values are formulas in the format of a function. See the object map below using the bit unit as an example:
polyconvert.data.bit = {
bit: f(x) = x,
byte: f(x) = x/8,
kilobyte: f(x) = x/8e+3,
megabyte: f(x) = x/8e+6,
gigabyte: f(x) = x/8e+9,
terabyte: f(x) = x/8e+12,
petabyte: f(x) = x/8e+15,
exabyte: f(x) = x/8e+18,
zettabyte: f(x) = x/8e+21,
yottabyte: f(x) = x/8e+24
}So, to use these functions, reference them through the polyconvert object and pass in the value you want to convert, like this:
polyconvert.data.bit.byte(100) // Returns 12.5 meaning that 100 bits equals 12.5 bytesTo reverse the conversion simply switch the order of the unit keys, like this:
polyconvert.data.byte.bit(12.5) // Returns 100 meaning that 12.5 bytes equals 100 bitsInformation about the testing approach:
The Polyconvert library includes comprehensive testing to ensure accuracy and reliability. The test suite validates conversions using two complementary approaches:
These tests verify that forward and reverse conversions return to the original value. For example:
- Convert 1 meter → feet → back to meters should equal 1 meter
- Convert 1 kilogram → pounds → back to kilograms should equal 1 kilogram
This ensures that conversion formulas are mathematically consistent and properly inverted.
These tests validate conversions against well-known values from reputable sources including:
- NIST (National Institute of Standards and Technology)
- International standards bodies (SI units, ISO standards)
- Widely accepted scientific references
Example validations:
- 1 meter = 3.28084 feet
- 0°C = 32°F (freezing point of water)
- 100°C = 212°F (boiling point of water)
- 1 kilogram = 2.20462 pounds
- 1 inch = 2.54 centimeters (exact)
- 1 atmosphere = 101325 pascals (exact)
The external validation tests cover all major categories including length, mass, temperature, volume, pressure, energy, data, time, speed, angle, area, and frequency conversions.
All tests can be run with the command:
npm test
This will execute both the internal consistency tests and external validation tests, providing comprehensive verification of all conversion formulas.
Information on how you can contribute to this project:
There are two main ways you can contribute to the Polyconvert library.
- Adding a new language, category or unit for conversion.
- Report an inaccurate formula or typo.
See below for more information on each of the options, also feel free to simply ask any question you like.
Currently this library supports JavaScript only but it would be nice to add more languages in the future! It would also be nice to add new categories and units for conversion since the current list is not universal, yet. If you need an idea for a new category to work on I suggest browsing the UnitConverters.net website, this site can also be useful for testing the accuracy of existing formulas.
If you would like to contribute in this way please start by opening a feature request. Once your work is done and ready for review you can proceed to opening a pull request.
I have done my best to ensure the accuracy of all the formulas but I am only human and I can easily make mistakes! If you find an inaccurate formula or typo please don't hesitate to open a bug report.
The library includes comprehensive testing to ensure accuracy:
-
Internal Consistency Tests: Validate that forward and reverse conversions return to the original value (e.g., meter → foot → meter should equal the original meter value).
-
External Validation Tests: Validate conversions against well-known values from reputable sources like NIST (National Institute of Standards and Technology) and international standards bodies. These tests verify that conversions match widely accepted reference values (e.g., 1 meter = 3.28084 feet, 0°C = 32°F, 1 kg = 2.20462 pounds).
All tests can be run with the command npm test.
If you have any other issues with the Polyconvert library then please open a general issue. Your efforts will improve the functionality and usability of this tool for everyone, thanks in advance!
A map of all the categories and units available for conversion:
Each category below is available as a top level key in the polyconvert object. An object map of each category is available below.
The Angle category provides 6 different units for conversion, see the object map below:
polyconvert.angle = {
degree: {...},
gradian: {...},
milliradian: {...},
radian: {...},
arcSecond: {...},
arcMinute: {...}
}The Area category provides 15 different units for conversion, see the object map below:
polyconvert.area = {
nanometerSq: {...},
micrometerSq: {...},
millimeterSq: {...},
centimeterSq: {...},
decimeterSq: {...},
meterSq: {...},
decameterSq: {...},
hectometerSq: {...},
kilometerSq: {...},
inchSq: {...},
footSq: {...},
yardSq: {...},
mileSq: {...},
acre: {...},
hectare: {...}
}The Data category provides 10 different units for conversion, see the object map below:
polyconvert.data = {
bit: {...},
byte: {...},
kilobyte: {...},
megabyte: {...},
gigabyte: {...},
terabyte: {...},
petabyte: {...},
exabyte: {...},
zettabyte: {...},
yottabyte: {...}
}The Density category provides 8 different units for conversion, see the object map below:
polyconvert.density = {
gramCentimeterCu: {...},
gramMeterCu: {...},
kilogramCentimeterCu: {...},
kilogramMeterCu: {...},
ounceInchCu: {...},
ounceFootCu: {...},
poundInchCu: {...},
poundFootCu: {...}
}The Dimension category provides 2 different units for conversion, see the object map below:
polyconvert.dimension = {
d2: {...},
d3: {...}
}The Energy category provides 11 different units for conversion, see the object map below:
polyconvert.energy = {
joule: {...},
kilojoule: {...},
wattHour: {...},
kilowattHour: {...},
electronvolt: {...},
calorie: {...},
kilocalorie: {...},
btu: {...},
footPound: {...},
// Power (time-dependent) units:
watt: {...},
kilowatt: {...}
}The Frequency category provides 4 different units for conversion, see the object map below:
polyconvert.frequency = {
hertz: {...},
kilohertz: {...},
megahertz: {...},
gigahertz: {...}
}The Illumination category provides 4 different units for conversion, see the object map below:
polyconvert.illumination = {
phot: {...},
lux: {...},
nox: {...},
flame: {...}
}The Length category provides 13 different units for conversion, see the object map below:
polyconvert.length = {
nanometer: {...},
micrometer: {...},
millimeter: {...},
centimeter: {...},
decimeter: {...},
meter: {...},
decameter: {...},
hectometer: {...},
kilometer: {...},
inch: {...},
foot: {...},
yard: {...},
mile: {...}
}The Magnetomotive category provides 5 different units for conversion, see the object map below:
polyconvert.magnetomotive = {
milliampere: {...},
ampere: {...},
abampere: {...},
kiloampere: {...},
gilbert: {...}
}The Mass category provides 13 different units for conversion, see the object map below:
polyconvert.mass = {
nanogram: {...},
microgram: {...},
milligram: {...},
centigram: {...},
decigram: {...},
gram: {...},
decagram: {...},
hectogram: {...},
kilogram: {...},
ounce: {...},
pound: {...},
stone: {...},
ton: {...}
}The Pressure category provides 5 different units for conversion, see the object map below:
polyconvert.pressure = {
bar: {...},
pascal: {...},
atmospheric: {...},
psi: {...},
torr: {...}
}The Radiation category provides 14 different units for conversion, see the object map below:
polyconvert.radiation = {
nanogray: {...},
microgray: {...},
milligray: {...},
centigray: {...},
decigray: {...},
gray: {...},
decagray: {...},
hectogray: {...},
kilogray: {...},
megagray: {...},
gigagray: {...},
teragray: {...},
petagray: {...},
exagray: {...}
}The Speed category provides 25 different units for conversion, see the object map below:
polyconvert.speed = {
millimeterSecond: {...},
millimeterMinute: {...},
millimeterHour: {...},
centimeterSecond: {...},
centimeterMinute: {...},
centimeterHour: {...},
meterSecond: {...},
meterMinute: {...},
meterHour: {...},
kilometerSecond: {...},
kilometerMinute: {...},
kilometerHour: {...},
inchSecond: {...},
inchMinute: {...},
inchHour: {...},
footSecond: {...},
footMinute: {...},
footHour: {...},
yardSecond: {...},
yardMinute: {...},
yardHour: {...},
mileSecond: {...},
mileMinute: {...},
mileHour: {...},
knots: {...}
}The Temperature category provides 3 different units for conversion, see the object map below:
polyconvert.temperature = {
celsius: {...},
fahrenheit: {...},
kelvin: {...}
}The Time category provides 13 different units for conversion, see the object map below:
polyconvert.time = {
nanosecond: {...},
microsecond: {...},
millisecond: {...},
second: {...},
minute: {...},
hour: {...},
day: {...},
week: {...},
month: {...},
year: {...},
decade: {...},
century: {...},
millennium: {...}
}The Volume category provides 21 different units for conversion, see the object map below:
polyconvert.volume = {
nanometerCu: {...},
micrometerCu: {...},
millimeterCu: {...},
centimeterCu: {...},
decimeterCu: {...},
meterCu: {...},
decameterCu: {...},
hectometerCu: {...},
kilometerCu: {...},
inchCu: {...},
footCu: {...},
yardCu: {...},
mileCu: {...},
milliliter: {...},
liter: {...},
teaspoon: {...},
tablespoon: {...},
cup: {...},
pint: {...},
quart: {...},
gallon: {...}
}Polyconvert is developed and maintained by @jgphilpott.