Show 0.00 instead of -0.00 in Unit::readout for tiny negatives

Found while testing crates/doc/src/units.rs:163-165 (readout)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-31 19:43:17 +07:00
committed by bflatastic
co-authored by Claude Haiku 5.5
parent a5a7c4638a
commit d0e1a045c1
2 changed files with 11 additions and 1 deletions
+7
View File
@@ -144,3 +144,10 @@ fn mixed_mul_div() {
assert_eq!(Unit::Points.parse("10*2/5"), Some(4.0));
assert_eq!(Unit::Points.parse("10*2/5 px"), Some(Unit::Pixels.to_pt(4.0)));
}
#[test]
fn readout_has_no_negative_zero() {
assert_eq!(Unit::Points.readout(-0.001), "0.00 pt");
assert_eq!(Unit::Points.readout(-0.0), "0.00 pt");
assert_eq!(Unit::Points.readout(-12.5), "-12.50 pt");
}
+4 -1
View File
@@ -161,7 +161,10 @@ impl Unit {
/// A point value as the canvas measurement labels show it: two decimals and the suffix
/// (`12.50 mm`).
pub fn readout(self, pt: f64) -> String {
format!("{:.2} {}", self.from_pt(pt), self.suffix())
let s = format!("{:.2}", self.from_pt(pt));
// A small negative value rounds to `-0.00`; show it as `0.00`.
let s = if s == "-0.00" { "0.00" } else { &s };
format!("{} {}", s, self.suffix())
}
}