diff --git a/README.md b/README.md index 4384935..f485013 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,9 @@ match string { ### Potential Improvements - Support for conversion between Power, Current, Resistance and Voltage. Multiplication and division is currently supported, but not conversions using sqrt or pow. +- Move to pure-rust decimal implementation + - `rust_decimal`: Only supports numbers up to ~1E+29 + - `bigdecimal`: Lacking math functions - E notation, like 2E+10 - Unit types - Currency: How to go about dynamically updating the weights? diff --git a/src/lib.rs b/src/lib.rs index 8d11db9..f56202e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ //! } //! ``` +use std::fmt::{self, Display}; use std::time::{Instant}; use decimal::d128; use crate::units::Unit; @@ -65,6 +66,17 @@ impl Number { } } } +impl Display for Number { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // 0.2/0.01 results in 2E+1, but if we add zero it becomes 20 + let fixed_value = self.value + d128!(0); + let output = match self.unit { + Unit::NoUnit => format!("{}", fixed_value), + unit => format!("{} {:?}", fixed_value, unit), + }; + return write!(f, "{}", output); + } +} #[derive(Clone, Debug)] /// Math operators like [`Multiply`](Operator::Multiply), parentheses, etc. diff --git a/src/main.rs b/src/main.rs index 557a420..9f14765 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ fn main() { match eval(&args[1], true, Unit::Celsius, verbose) { Ok(answer) => { if !verbose { - println!("{} {:?}", answer.value, answer.unit) + println!("{}", answer); } }, Err(e) => {