Skip to content

Commit

Permalink
Fix numbers unnecessarily displayed in E notation
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed Jun 10, 2021
1 parent 7154879 commit e4fc479
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! }
//! ```
use std::fmt::{self, Display};
use std::time::{Instant};
use decimal::d128;
use crate::units::Unit;
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit e4fc479

Please sign in to comment.