Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

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

NetterSilverd0e1a045c166
diff --git a/crates/doc/src/tests/units.rs b/crates/doc/src/tests/units.rs
index 909a0adf3..88075a5dc 100644
--- a/crates/doc/src/tests/units.rs
+++ b/crates/doc/src/tests/units.rs
@@ -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");
+}
diff --git a/crates/doc/src/units.rs b/crates/doc/src/units.rs
index 2c1fc8653..6a5edf75a 100644
--- a/crates/doc/src/units.rs
+++ b/crates/doc/src/units.rs
@@ -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())
}
}