Skip to content

JonasEngstrom/si-prefixes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust

SI Prefixes

SI Prefixes is a crate for converting between units using different SI prefixes. It is developed as a dependency to be used in other crates requiring unit conversion. The prefixes are based on The International System of Units1.

Usage

The crate includes the factors, names, and symbols listed below for use in calculations and output formatting. It also includes a method to calculate conversion constants that can be multiplied with a value in order to change its prefix.

Usage revolves around the Prefix enum. For example the prefix kilo is represented as Prefix::Kilo.

Note that, even though Bureau International des Poids et Mesures uses lower-case letters for the entire prefixes, the enum variants used in the source code have initial capital letters, as to conform to The Rust Style Guide.

Getting a Prefix Name

use si_prefixes::Prefix;

let prefix_name = Prefix::Micro.name();

assert_eq!(prefix_name.unwrap(), "micro");
use si_prefixes::Prefix;

let prefix_name = Prefix::None.name();

assert_eq!(prefix_name, None);

Getting a Prefix Symbol

use si_prefixes::Prefix;

let prefix_symbol = Prefix::Mega.symbol();

assert_eq!(prefix_symbol.unwrap(), "M");
use si_prefixes::Prefix;

let prefix_symbol = Prefix::None.symbol();

assert_eq!(prefix_symbol, None);

Getting a Prefix Factor

use si_prefixes::Prefix;

let prefix_factor = Prefix::Kilo.factor();

assert_eq!(prefix_factor, 1_000f64);

Getting a Prefix Conversion Constant

Converting from One Prefix to Another

use si_prefixes::Prefix;

let centimeters = 50f64;
let decimeters = centimeters * Prefix::conversion_constant(Prefix::Centi, Prefix::Deci);

assert_eq!(decimeters, 5f64);

Adding a Prefix

use si_prefixes::Prefix;

let meters = 0.5f64;
let decimeters = meters * Prefix::conversion_constant(Prefix::None, Prefix::Deci);

assert_eq!(decimeters, 5f64);

Removing a Prefix

use si_prefixes::Prefix;

let decimeters = 5f64;
let meters = decimeters * Prefix::conversion_constant(Prefix::Deci, Prefix::None);

assert_eq!(meters, 0.5f64);

Included Prefixes

Apart from strict SI system prefixes, that refer to powers of 10, the crate also includes prefixes referring to powers of 2. The included prefixes are listed in the tables below.

SI Prefixes (Referring to Powers of 10)

Name Symbol Factor
deca da 101
hecto h 102
kilo k 103
mega M 106
giga G 109
tera T 1012
peta P 1015
exa E 1018
zetta Z 1021
yotta Y 1024
ronna R 1027
quetta Q 1030
deci d 10-1
centi c 10-2
milli m 10-3
micro µ 10-6
nano n 10-9
pico p 10-12
femto f 10-15
atto a 10-18
zepto z 10-21
yocto y 10-24
ronto r 10-27
quecto q 10-30

Prefixes Referring to Powers of 2

Name Symbol Factor
kibi Ki 210
mebi Mi 220
gibi Gi 230
tebi Ti 240
pebi Pi 250
exbi Ei 260
zebi Zi 270
yobi Yi 280
robi Ri 290
quebi Qi 2100

References

  1. Bureau International des Poids et Mesures. (2025). Le Système international d’unités/The International System of Units. 9th edition. https://doi.org/10.59161/AUEZ1291

About

A crate for converting between units using different SI prefixes.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages