Compare commits

...

3 Commits

Author SHA1 Message Date
Juhani Krekelä 79634cb944 Add README.md 2023-06-01 03:31:04 +03:00
Juhani Krekelä b13191050f Add additional ways to spell US cup 2023-06-01 03:09:00 +03:00
Juhani Krekelä 4eb55ac7b8 Change terminology weight → mass 2023-06-01 03:00:10 +03:00
5 changed files with 111 additions and 7 deletions

100
README.md Normal file
View 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 58″
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.

View File

@ -48,7 +48,7 @@ fn get_conversion(unit: NonMetric) -> Conversion {
from: inch_from,
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::Metre },
},
// Weight
// Mass
NonMetric::Ounce => Conversion {
offset: 0.0,
from: 16.0 * pound_from,
@ -189,7 +189,7 @@ mod test {
}
#[test]
fn weight() {
fn mass() {
let tests = [
Test(NonMetric::Ounce, 28.349523125),
Test(NonMetric::Pound, 453.59237),

View File

@ -73,7 +73,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
NonMetric::Foot => "feet",
NonMetric::Yard => "yards",
NonMetric::Mile => "miles",
// Weight
// Mass
NonMetric::Ounce => "ounces",
NonMetric::Pound => "pounds",
NonMetric::Stone => "stones",
@ -127,7 +127,7 @@ mod test {
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 mile"), Ok("1.609 km".to_string()));
// Weight
// Mass
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 st"), Ok("6.35 kg".to_string()));

View File

@ -83,7 +83,7 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
"mi" => Ok(NonMetric::Mile),
"m" => Ok(NonMetric::Mile),
// Weight
// Mass
"ounce" => Ok(NonMetric::Ounce),
"ounces" => 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 cup" => Ok(NonMetric::USCup),
"us cups" => Ok(NonMetric::USCup),
"cup" => Ok(NonMetric::USCup),
"cups" => Ok(NonMetric::USCup),
"US liquid pint" => Ok(NonMetric::USLiquidPint),
"US liquid pints" => Ok(NonMetric::USLiquidPint),
@ -459,7 +461,7 @@ mod test {
"m",
]);
// Weight
// Mass
test_units(NonMetric::Ounce, &[
"ounce",
"ounces",
@ -678,6 +680,8 @@ mod test {
"US cups",
"us cup",
"us cups",
"cup",
"cups",
]);
test_units(NonMetric::USLiquidPint, &[

View File

@ -15,7 +15,7 @@ pub enum NonMetric {
Foot,
Yard,
Mile,
// Weight
// Mass
Ounce,
Pound,
Stone,