Unit Convert

A Dart unit converter to convert between numerous units easily.

Installing

Add it to your pubspec.yaml file:

dependencies:
  unit_convert: ^0.1.3

Install packages from the command line

flutter packages get

If you like this package, consider supporting it by giving it a star on GitHub and a like on pub.dev :heart:

Usage

Conversions

You can use the convert function to convert between most units:

import 'package:unit_convert/unit_convert.dart';

//                     (from)    (to)
double result = convert(celsius, fahrenheit, 100);

or using the to method on each unit:

double result = celsius.to(fahrenheit, 100);

or shorthand using function invocation:

double result = celsius(fahrenheit, 100);

or using from for more clarity:

double result = from(celsius).to(fahrenheit, 100);

The most commonly used units are already imported by default. If you need to convert between less commonly used units, please use the enum of the unit category:

double result = convert(LengthUnit.astronomicalUnit, LengthUnit.lightYear, 100000);
double result = TimeUnit.leapYear.to(TimeUnit.quindecinnal, 10);
double result = from(EnergyUnit.kilocalorie).to(EnergyUnit.wattHour, 1000);

Available Units

The following unit categories are available:

CategoryExamples Units
AngleUnitdegree, radian, grad, minute
AreaUnitsquareMeter, squareMile, hectare
EnergyUnitjoule, wattHour, calorie
ForceUnitnewton, poundForce, dyne
LengthUnitkilometer, mile, lightYear
NumberUnitbinary, hexadecimal, base-4
PowerUnitwatt, horsepower, caloriesPerHour
PressureUnitbar, psi, pascal, hectopascal
SpeedUnitkilometerPerHour, yardPerMinute
StorageUnitbyte, kilobit, terabyte, dvd
TemperatureUnitcelsius, kelvin, fahrenheit
TimeUnitday, millenium, plankTime
VolumeUnitgallon, barrel, milliliter
WeightUnitpound, ton, kilogram, ounce

Localization

Each unit has a name and a symbol associated with it. You can set or get the locale for name and symbol using:

Converter.locale = 'en';

Currently supported languages are:

LanguageCode
Englishen_GB
Germande_DE

Persistence

Each unit has a unique id that you can use for persistence to local storage. You can then call parse on the unit enum to retrieve again the correct converter.

final id = TemperatureUnit.celsius.id;

final celsius = TemperatureUnit.parse('celsius');

Libraries

unit_converter
A library to easily convert between different units