Properly handle trailing zeroes

This fixes an edge case in which there were trailing zeros that resulted
from the decimal having a non-zero part outside the precision range.

E.g. if precision were 10, `0.00000000000000000001` would be printed as `0.0000000000`.

This issue occured often when doing unit conversions between cm and in
(and similarly between dpcm and dpi).
This commit is contained in:
ConnorSkees 2020-03-18 10:34:17 -04:00
parent efaa33088b
commit 0fe97993f4
2 changed files with 16 additions and 8 deletions

View File

@ -124,11 +124,11 @@ impl Display for Number {
} }
} }
if frac != BigRational::from_integer(BigInt::from(0)) { if frac != BigRational::from_integer(BigInt::from(0)) {
let f = (frac * BigRational::from_integer(BigInt::from(10))) let end = (frac * BigRational::from_integer(BigInt::from(10)))
.round() .round()
.abs() .abs()
.to_integer(); .to_integer();
if f == BigInt::from(10) { if end == BigInt::from(10) {
loop { loop {
match dec.pop().unwrap() { match dec.pop().unwrap() {
'9' => continue, '9' => continue,
@ -142,8 +142,19 @@ impl Display for Number {
} }
} }
} }
} else if end == BigInt::from(0) {
loop {
match dec.pop().unwrap() {
'0' => continue,
'.' => break,
c => {
dec.push(c);
break;
}
}
}
} else { } else {
write!(dec, "{}", f)?; write!(dec, "{}", end)?;
} }
} }
} }

View File

@ -85,9 +85,7 @@ test_unit_addition!(in, q, "1.0098425197");
test_unit_addition!(in, pt, "1.0138888889"); test_unit_addition!(in, pt, "1.0138888889");
test_unit_addition!(in, px, "1.0104166667"); test_unit_addition!(in, px, "1.0104166667");
// fails with output `3.5400000000` test_unit_addition!(cm, in, "3.54");
// oddly, `3.5400000000` does normally get changed to `3.54`
// test_unit_addition!(cm, in, "3.54");
test_unit_addition!(cm, cm, "2"); test_unit_addition!(cm, cm, "2");
test_unit_addition!(cm, pc, "1.4233333333"); test_unit_addition!(cm, pc, "1.4233333333");
test_unit_addition!(cm, mm, "1.1"); test_unit_addition!(cm, mm, "1.1");
@ -168,8 +166,7 @@ test_unit_addition!(kHz, Hz, "1.001");
test_unit_addition!(kHz, kHz, "2"); test_unit_addition!(kHz, kHz, "2");
test_unit_addition!(dpi, dpi, "2"); test_unit_addition!(dpi, dpi, "2");
// see above for issues with cm and trailing zeroes test_unit_addition!(dpi, dpcm, "3.54");
// test_unit_addition!(dpi, dpcm, "3.54");
test_unit_addition!(dpi, dppx, "97"); test_unit_addition!(dpi, dppx, "97");
test_unit_addition!(dpcm, dpi, "1.3937007874"); test_unit_addition!(dpcm, dpi, "1.3937007874");