Skip to main content
This guide will walk you through performing your first unit conversions with Univerto.

Basic Conversion

1

Install Univerto

First, install the package in your project:
npm install univerto
For more installation options, see the Installation guide.
2

Import a Converter

Import the unit converter you need. Univerto uses subpath imports to keep your bundle size minimal:
import { TIME_UNIT, TimeUnitConverter } from 'univerto/time'
Each converter comes with its own unit constants (e.g., TIME_UNIT, LENGTH_UNIT) and converter class (e.g., TimeUnitConverter, LengthUnitConverter).
3

Perform a Conversion

Use the fluent API to convert between units:
const milliseconds = TimeUnitConverter.from(1, TIME_UNIT.HOUR)
  .to(TIME_UNIT.MILLISECOND)
  .convert()

console.log(milliseconds) // 3600000
The API follows a simple pattern:
  1. .from(value, unit) - Specify the source value and unit
  2. .to(unit) - Specify the target unit
  3. .convert() - Get the converted numeric result
4

Explore More Converters

Try different unit categories:
import { LENGTH_UNIT, LengthUnitConverter } from 'univerto/length'
import { MASS_UNIT, MassUnitConverter } from 'univerto/mass'

// Convert length
const meters = LengthUnitConverter.from(5, LENGTH_UNIT.MILE)
  .to(LENGTH_UNIT.KILOMETER)
  .convert()

console.log(meters) // 8.04672

// Convert mass
const kilograms = MassUnitConverter.from(150, MASS_UNIT.POUND)
  .to(MASS_UNIT.KILOGRAM)
  .convert()

console.log(kilograms) // 68.0388555

Conversion Methods

Univerto provides three methods for obtaining conversion results:

1. convert() - Get Numeric Result

The most common method returns a JavaScript number:
import { TIME_UNIT, TimeUnitConverter } from 'univerto/time'

const result = TimeUnitConverter.from(1, TIME_UNIT.HOUR)
  .to(TIME_UNIT.MILLISECOND)
  .convert()

console.log(result) // 3600000

2. convertToFraction() - Get Fraction Object

Get the result as a fraction to avoid floating-point precision issues:
import { TIME_UNIT, TimeUnitConverter } from 'univerto/time'

const fraction = TimeUnitConverter.from(1, TIME_UNIT.HOUR)
  .to(TIME_UNIT.MILLISECOND)
  .convertToFraction()

console.log(fraction) // { numerator: 3600000, denominator: 1 }
Use convertToFraction() when you need to pass the result to a high-precision math library like decimal.js or big.js.

3. convertToRational() - Get Rational Object

Get the internal rational representation with additional methods:
import { TIME_UNIT, TimeUnitConverter } from 'univerto/time'

const rational = TimeUnitConverter.from(1, TIME_UNIT.HOUR)
  .to(TIME_UNIT.MILLISECOND)
  .convertToRational()

// Rational objects have methods like multiply, divide, add, subtract
console.log(rational.toNumber()) // 3600000
console.log(rational.toFraction()) // { numerator: 3600000, denominator: 1 }

High-Precision Arithmetic

For applications requiring extreme precision, combine Univerto with a decimal library:
import { TIME_UNIT, TimeUnitConverter } from 'univerto/time'
import { Decimal } from 'decimal.js'

const fraction = TimeUnitConverter.from(1, TIME_UNIT.HOUR)
  .to(TIME_UNIT.MILLISECOND)
  .convertToFraction()

const result = new Decimal(fraction.numerator)
  .div(fraction.denominator)

console.log(result.toString()) // "3600000"
Univerto internally represents all conversions as fractions to avoid floating-point errors. When you call convert(), it performs the final division to give you a JavaScript number.

Common Examples

Here are some real-world conversion examples:
import { TIME_UNIT, TimeUnitConverter } from 'univerto/time'

// Hours to minutes
const minutes = TimeUnitConverter.from(2.5, TIME_UNIT.HOUR)
  .to(TIME_UNIT.MINUTE)
  .convert() // 150

// Days to seconds
const seconds = TimeUnitConverter.from(1, TIME_UNIT.DAY)
  .to(TIME_UNIT.SECOND)
  .convert() // 86400

Next Steps

Core Concepts

Learn about the internal architecture and design decisions

Unit Converters

Explore all available unit converters and their supported units

Build docs developers (and LLMs) love