Fix numbers unnecessarily displayed in E notation

This commit is contained in:
Kasper 2021-06-10 21:36:14 +02:00
parent 71548793c5
commit e4fc479091
3 changed files with 16 additions and 1 deletions

View File

@ -164,6 +164,9 @@ match string {
### Potential Improvements ### Potential Improvements
- Support for conversion between Power, Current, Resistance and Voltage. Multiplication and division is currently supported, but not conversions using sqrt or pow. - 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 - E notation, like 2E+10
- Unit types - Unit types
- Currency: How to go about dynamically updating the weights? - Currency: How to go about dynamically updating the weights?

View File

@ -22,6 +22,7 @@
//! } //! }
//! ``` //! ```
use std::fmt::{self, Display};
use std::time::{Instant}; use std::time::{Instant};
use decimal::d128; use decimal::d128;
use crate::units::Unit; 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)] #[derive(Clone, Debug)]
/// Math operators like [`Multiply`](Operator::Multiply), parentheses, etc. /// Math operators like [`Multiply`](Operator::Multiply), parentheses, etc.

View File

@ -14,7 +14,7 @@ fn main() {
match eval(&args[1], true, Unit::Celsius, verbose) { match eval(&args[1], true, Unit::Celsius, verbose) {
Ok(answer) => { Ok(answer) => {
if !verbose { if !verbose {
println!("{} {:?}", answer.value, answer.unit) println!("{}", answer);
} }
}, },
Err(e) => { Err(e) => {