Compare commits
3 commits
db674d1419
...
79634cb944
Author | SHA1 | Date | |
---|---|---|---|
|
79634cb944 | ||
|
b13191050f | ||
|
4eb55ac7b8 |
5 changed files with 111 additions and 7 deletions
100
README.md
Normal file
100
README.md
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
Metrify
|
||||||
|
=======
|
||||||
|
Metrify is a tool for converting commonly-used non-metric units to metric.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
Simplest way to use metrify is to invoke it with an expression in terms of
|
||||||
|
non-metric units as its arguments:
|
||||||
|
|
||||||
|
metrify 1.4 short tons
|
||||||
|
|
||||||
|
The expression can contain several number + unit sequences, which are summed
|
||||||
|
together if the units are compatible:
|
||||||
|
|
||||||
|
metrify 5′8″
|
||||||
|
metrify 1 acre 15 square feet
|
||||||
|
|
||||||
|
You can also invoke metrify without arguments, in which case it will give you a
|
||||||
|
prompt to type the expression into.
|
||||||
|
|
||||||
|
Result display
|
||||||
|
--------------
|
||||||
|
Metrify will pick the appropriate prefix for the given result and unit
|
||||||
|
automatically. The value is rounded so that results <1 have 4 digits, <10 3
|
||||||
|
digits, <100 2 digits, <1000 1 digit and ≥1000 no digits after the decimal
|
||||||
|
point. The digits before the decimal point are grouped into groups of three.
|
||||||
|
|
||||||
|
0.4929 ml
|
||||||
|
1.609 km
|
||||||
|
28.35 g
|
||||||
|
907.2 kg
|
||||||
|
28 317 cm³
|
||||||
|
|
||||||
|
Supported units
|
||||||
|
---------------
|
||||||
|
|
||||||
|
### Length units based on the international yard
|
||||||
|
|
||||||
|
* inch (in)
|
||||||
|
* foot (ft)
|
||||||
|
* yard (yd)
|
||||||
|
* mile (mi)
|
||||||
|
|
||||||
|
### Mass units based on the international pound
|
||||||
|
|
||||||
|
* ounce (oz)
|
||||||
|
* pound (lb)
|
||||||
|
* stone (st)
|
||||||
|
* short ton
|
||||||
|
* long ton
|
||||||
|
|
||||||
|
### Temperature
|
||||||
|
|
||||||
|
* Degrees Fahrenheit (°F)
|
||||||
|
|
||||||
|
### Area units based on the international yard
|
||||||
|
|
||||||
|
* square inch (in²)
|
||||||
|
* square foot (ft²)
|
||||||
|
* acre (ac)
|
||||||
|
* square mile (mi²)
|
||||||
|
|
||||||
|
### Volume units based on the international yard
|
||||||
|
|
||||||
|
* cubic inch (in³)
|
||||||
|
* cubic foot (ft³)
|
||||||
|
|
||||||
|
### Imperial fluid volume units
|
||||||
|
|
||||||
|
* imperial fluid ounce (imp fl oz)
|
||||||
|
* imperial pint (imp pt)
|
||||||
|
* imperial quart (imp qt)
|
||||||
|
* imperial gallon (imp gal)
|
||||||
|
|
||||||
|
### US customary fluid volume units
|
||||||
|
|
||||||
|
* US teaspoon (tsp)
|
||||||
|
* US tablespoon (tbsp)
|
||||||
|
* US fluid ounce (us fl oz)
|
||||||
|
* US cup (cup)
|
||||||
|
* US pint (us pt)
|
||||||
|
* US quart (us qt)
|
||||||
|
* US gallon (us gal)
|
||||||
|
|
||||||
|
Why should I use metrify instead of units(1)?
|
||||||
|
---------------------------------------------
|
||||||
|
Metrify is more special-purpose than units(1), which allows it to present the
|
||||||
|
user a nicer interface for the purposes it does support. For example with
|
||||||
|
metrify you do not need to explicitly tell it the unit you are converting to,
|
||||||
|
because due to its requirements (conversion is always from non-metric to metric)
|
||||||
|
it can automatically pick the correct one.
|
||||||
|
|
||||||
|
Building and installation
|
||||||
|
-------------------------
|
||||||
|
You can build and test out metrify with `cargo run`. If you want to install it
|
||||||
|
locally, you can use `cargo install --path .` in the source directory.
|
||||||
|
|
||||||
|
License
|
||||||
|
-------
|
||||||
|
Metrify is under Creative Commons Zero 1.0.
|
|
@ -48,7 +48,7 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
||||||
from: inch_from,
|
from: inch_from,
|
||||||
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::Metre },
|
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::Metre },
|
||||||
},
|
},
|
||||||
// Weight
|
// Mass
|
||||||
NonMetric::Ounce => Conversion {
|
NonMetric::Ounce => Conversion {
|
||||||
offset: 0.0,
|
offset: 0.0,
|
||||||
from: 16.0 * pound_from,
|
from: 16.0 * pound_from,
|
||||||
|
@ -189,7 +189,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn weight() {
|
fn mass() {
|
||||||
let tests = [
|
let tests = [
|
||||||
Test(NonMetric::Ounce, 28.349523125),
|
Test(NonMetric::Ounce, 28.349523125),
|
||||||
Test(NonMetric::Pound, 453.59237),
|
Test(NonMetric::Pound, 453.59237),
|
||||||
|
|
|
@ -73,7 +73,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
|
||||||
NonMetric::Foot => "feet",
|
NonMetric::Foot => "feet",
|
||||||
NonMetric::Yard => "yards",
|
NonMetric::Yard => "yards",
|
||||||
NonMetric::Mile => "miles",
|
NonMetric::Mile => "miles",
|
||||||
// Weight
|
// Mass
|
||||||
NonMetric::Ounce => "ounces",
|
NonMetric::Ounce => "ounces",
|
||||||
NonMetric::Pound => "pounds",
|
NonMetric::Pound => "pounds",
|
||||||
NonMetric::Stone => "stones",
|
NonMetric::Stone => "stones",
|
||||||
|
@ -127,7 +127,7 @@ mod test {
|
||||||
assert_eq!(run("1 ft"), Ok("30.48 cm".to_string()));
|
assert_eq!(run("1 ft"), Ok("30.48 cm".to_string()));
|
||||||
assert_eq!(run("1 yard"), Ok("91.44 cm".to_string()));
|
assert_eq!(run("1 yard"), Ok("91.44 cm".to_string()));
|
||||||
assert_eq!(run("1 mile"), Ok("1.609 km".to_string()));
|
assert_eq!(run("1 mile"), Ok("1.609 km".to_string()));
|
||||||
// Weight
|
// Mass
|
||||||
assert_eq!(run("1 oz"), Ok("28.35 g".to_string()));
|
assert_eq!(run("1 oz"), Ok("28.35 g".to_string()));
|
||||||
assert_eq!(run("1 lb"), Ok("453.6 g".to_string()));
|
assert_eq!(run("1 lb"), Ok("453.6 g".to_string()));
|
||||||
assert_eq!(run("1 st"), Ok("6.35 kg".to_string()));
|
assert_eq!(run("1 st"), Ok("6.35 kg".to_string()));
|
||||||
|
|
|
@ -83,7 +83,7 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
||||||
"mi" => Ok(NonMetric::Mile),
|
"mi" => Ok(NonMetric::Mile),
|
||||||
"m" => Ok(NonMetric::Mile),
|
"m" => Ok(NonMetric::Mile),
|
||||||
|
|
||||||
// Weight
|
// Mass
|
||||||
"ounce" => Ok(NonMetric::Ounce),
|
"ounce" => Ok(NonMetric::Ounce),
|
||||||
"ounces" => Ok(NonMetric::Ounce),
|
"ounces" => Ok(NonMetric::Ounce),
|
||||||
"oz" => Ok(NonMetric::Ounce),
|
"oz" => Ok(NonMetric::Ounce),
|
||||||
|
@ -263,6 +263,8 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
||||||
"US cups" => Ok(NonMetric::USCup),
|
"US cups" => Ok(NonMetric::USCup),
|
||||||
"us cup" => Ok(NonMetric::USCup),
|
"us cup" => Ok(NonMetric::USCup),
|
||||||
"us cups" => Ok(NonMetric::USCup),
|
"us cups" => Ok(NonMetric::USCup),
|
||||||
|
"cup" => Ok(NonMetric::USCup),
|
||||||
|
"cups" => Ok(NonMetric::USCup),
|
||||||
|
|
||||||
"US liquid pint" => Ok(NonMetric::USLiquidPint),
|
"US liquid pint" => Ok(NonMetric::USLiquidPint),
|
||||||
"US liquid pints" => Ok(NonMetric::USLiquidPint),
|
"US liquid pints" => Ok(NonMetric::USLiquidPint),
|
||||||
|
@ -459,7 +461,7 @@ mod test {
|
||||||
"m",
|
"m",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Weight
|
// Mass
|
||||||
test_units(NonMetric::Ounce, &[
|
test_units(NonMetric::Ounce, &[
|
||||||
"ounce",
|
"ounce",
|
||||||
"ounces",
|
"ounces",
|
||||||
|
@ -678,6 +680,8 @@ mod test {
|
||||||
"US cups",
|
"US cups",
|
||||||
"us cup",
|
"us cup",
|
||||||
"us cups",
|
"us cups",
|
||||||
|
"cup",
|
||||||
|
"cups",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test_units(NonMetric::USLiquidPint, &[
|
test_units(NonMetric::USLiquidPint, &[
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub enum NonMetric {
|
||||||
Foot,
|
Foot,
|
||||||
Yard,
|
Yard,
|
||||||
Mile,
|
Mile,
|
||||||
// Weight
|
// Mass
|
||||||
Ounce,
|
Ounce,
|
||||||
Pound,
|
Pound,
|
||||||
Stone,
|
Stone,
|
||||||
|
|
Loading…
Reference in a new issue