Math.js: Units and Conversions
JS Calc, through Math.js, provides robust support for calculations involving physical units and seamless conversions between compatible units.
Defining Units
You can define a quantity with a unit in two primary ways:
-
Directly append the unit string to the value:
5 kg
,100 cm
,25 degC
,60 miles/hour or 60 mi/h
-
Using the
unit()
function: This can be useful for clarity or when the unit string might be ambiguous or part of a variable. Syntax:unit(value, unitString)
orunit(unitString)
for creating a unit without a value (less common for direct input).unit(2.5, "m") = 2.5 m
pressure = unit(101325, "Pa") then pressure = 101325 Pa
Calculations with Units
When you perform arithmetic operations with compatible units, JS Calc handles the unit calculations automatically:
> 5 meter + 200 cm
7 m
> 1 kg * 9.8 m/s^2
9.8 kg m / s^2
> (10 ft * 3 ft) / 2
15 ft^2
> 100 km / 2 hour
50 km / h
If units are incompatible for an operation (e.g., adding meters to kilograms), JS Calc will throw an error.
Unit Conversions
Math.js offers powerful and intuitive unit conversion using the to
or in
keywords.
Syntax:
* expression to targetUnit
* expression in targetUnit
The keywords to
and in
are interchangeable for conversion.
> 5 inch to cm
12.7 cm
> 100 km/h in m/s
26.8224 m/s
> (5 kg * 9.8 m/s^2) to lbf // Convert newton to pound-force
11.0151 lbf
> 27 degC to degF
80.6 degF
> 1 day in seconds
86400 s
> 1 acre to m^2
4046.85642 m^2
You can also perform calculations and convert the final result using to
or in
:
> 2 ft + 3 in to cm
68.58 cm
> 500 km - 2498 m in km
497.502 km
Available Units
Math.js supports a vast range of units across various dimensions. Here are some common categories:
- Length:
meter
(m
),inch
(in
),foot
(ft
),yard
(yd
),mile
(mi
),kilometer
(km
),centimeter
(cm
),millimeter
(mm
),angstrom
,link
,rod
,chain
, etc. - Mass:
gram
(g
),kilogram
(kg
),tonne
(t
),ounce
(oz
),pound
(lb
,lbm
),stone
, etc. - Time:
second
(s
),minute
(min
),hour
(h
),day
,week
,month
,year
, etc. - Temperature:
kelvin
(K
),celsius
(degC
),fahrenheit
(degF
),rankine
(degR
).- Note: Temperature conversions handle differences in scale and offset correctly (e.g.,
0 degC to degF
).
- Note: Temperature conversions handle differences in scale and offset correctly (e.g.,
- Angles:
radian
(rad
),degree
(deg
),gradian
(grad
),cycle
,arcsecond
(arcsec
),arcminute
(arcmin
).- Trigonometric functions in Math.js expect angles in radians by default. You can use degree units directly:
sin(30 deg)
.
- Trigonometric functions in Math.js expect angles in radians by default. You can use degree units directly:
- Area:
m2
(square meter),sqin
(square inch),acre
,hectare
, etc. - Volume:
m3
(cubic meter),litre
(L
,l
),cc
(cubic centimeter),cuin
(cubic inch),gallon
(gal
),pint
, etc. - Force:
newton
(N
),dyne
,poundforce
(lbf
),kip
, etc. - Energy:
joule
(J
),erg
,Wh
(watt-hour),kWh
(kilowatt-hour),BTU
,electronvolt
(eV
), etc. - Power:
watt
(W
),hp
(horsepower), etc. - Pressure:
pascal
(Pa
),psi
,atm
(atmosphere),bar
,torr
,mmHg
, etc. - Electric Units:
ampere
(A
),coulomb
(C
),volt
(V
),ohm
,farad
(F
),henry
(H
),siemens
(S
),weber
(Wb
),tesla
(T
). - Data Storage:
bit
(b
),byte
(B
), with binary prefixes (KiB
,MiB
,GiB
,TiB
) and SI prefixes (kB
,MB
,GB
,TB
). - ...and many more.
For a comprehensive list and details on defining custom units, refer to the official Math.js Units Documentation.
Unit Simplification
Math.js often simplifies resulting units:
> 1 N * 1 m
1 J // Newton-meter is a Joule
> 1 kg * m/s^2
1 N // Base units of Newton