Skip to content

Commit e4fc479

Browse files
Fix numbers unnecessarily displayed in E notation
1 parent 7154879 commit e4fc479

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ match string {
164164

165165
### Potential Improvements
166166
- Support for conversion between Power, Current, Resistance and Voltage. Multiplication and division is currently supported, but not conversions using sqrt or pow.
167+
- Move to pure-rust decimal implementation
168+
- `rust_decimal`: Only supports numbers up to ~1E+29
169+
- `bigdecimal`: Lacking math functions
167170
- E notation, like 2E+10
168171
- Unit types
169172
- Currency: How to go about dynamically updating the weights?

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//! }
2323
//! ```
2424
25+
use std::fmt::{self, Display};
2526
use std::time::{Instant};
2627
use decimal::d128;
2728
use crate::units::Unit;
@@ -65,6 +66,17 @@ impl Number {
6566
}
6667
}
6768
}
69+
impl Display for Number {
70+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71+
// 0.2/0.01 results in 2E+1, but if we add zero it becomes 20
72+
let fixed_value = self.value + d128!(0);
73+
let output = match self.unit {
74+
Unit::NoUnit => format!("{}", fixed_value),
75+
unit => format!("{} {:?}", fixed_value, unit),
76+
};
77+
return write!(f, "{}", output);
78+
}
79+
}
6880

6981
#[derive(Clone, Debug)]
7082
/// Math operators like [`Multiply`](Operator::Multiply), parentheses, etc.

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
match eval(&args[1], true, Unit::Celsius, verbose) {
1515
Ok(answer) => {
1616
if !verbose {
17-
println!("{} {:?}", answer.value, answer.unit)
17+
println!("{}", answer);
1818
}
1919
},
2020
Err(e) => {

0 commit comments

Comments
 (0)