convert-units 0.1.0

Convert between units of measurement (length, mass, volume, temperature, time, digital, ...) with a fluent API. A faithful port of the convert-units npm package. Zero dependencies.
Documentation
  • Coverage
  • 100%
    29 out of 29 items documented1 out of 16 items with examples
  • Size
  • Source code size: 73.33 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 750.16 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/convert-units
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

convert-units

All Contributors

crates.io docs.rs CI license

Convert between units of measurement — length, area, mass, volume, temperature, time, digital, speed, pressure, energy, power, frequency, angle, and more (23 measures, 185 units), with a fluent API.

A faithful Rust port of the popular convert-units npm package.

  • Zero dependencies
  • to_best to pick the most readable unit
  • describe, list, measures, and possibilities introspection
  • Differential-tested against the reference convert-units implementation (all 185 × 185 unit pairs)

Install

[dependencies]
convert-units = "0.1"

Usage

use convert_units::convert;

assert_eq!(convert(1.0).from("kg").unwrap().to("lb").unwrap(), 2.2046244201837775);
assert_eq!(convert(100.0).from("cm").unwrap().to("m").unwrap(), 1.0);
assert_eq!(convert(0.0).from("C").unwrap().to("F").unwrap(), 32.0);
assert_eq!(convert(1.0).from("GB").unwrap().to("MB").unwrap(), 1024.0);

// Cross-measure conversions are rejected:
assert!(convert(1.0).from("kg").unwrap().to("m").is_err());

Pick the most appropriate unit:

use convert_units::convert;

let best = convert(12000.0).from("mm").unwrap().to_best().unwrap();
assert_eq!((best.val, best.unit), (12.0, "m"));

Introspect the available units:

use convert_units::{convert, describe, measures, list};

assert_eq!(describe("kg").unwrap().singular, "Kilogram");
assert_eq!(measures().len(), 23);
assert_eq!(list(Some("length")).len(), 9);
assert_eq!(convert(1.0).from("kg").unwrap().possibilities(), ["mcg", "mg", "g", "kg", "mt", "oz", "lb", "t"]);

Contributors ✨

This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

License

Licensed under either of MIT or Apache-2.0 at your option.