diff --git a/core/src/number.rs b/core/src/number.rs index 0119850..7db4af6 100644 --- a/core/src/number.rs +++ b/core/src/number.rs @@ -379,6 +379,7 @@ impl<'a> NumberPartsFmt<'a> { let parts = self.number; + let mut last_was_ws = true; for tok in parse_pattern(self.pattern) { match tok { PatternToken::Exact => { @@ -419,6 +420,7 @@ impl<'a> NumberPartsFmt<'a> { tokens.push(Span::plain("* ")); tokens.push(Span::number(f)); tokens.push(Span::plain(" ")); + last_was_ws = true; } let mut first = true; for (dim, &exp) in unit { @@ -434,10 +436,15 @@ impl<'a> NumberPartsFmt<'a> { if exp != 1 { tokens.push(Span::pow(format!("^{}", exp))); } + last_was_ws = false; } } if !frac.is_empty() || parts.divfactor.is_some() { - tokens.push(Span::plain(" /")); + if last_was_ws { + tokens.push(Span::plain("/")); + } else { + tokens.push(Span::plain(" /")); + } if let Some(ref d) = parts.divfactor { tokens.push(Span::plain(" ")); tokens.push(Span::number(d)); @@ -536,11 +543,14 @@ impl<'a> NumberPartsFmt<'a> { }, PatternToken::Whitespace => { tokens.push(Span::plain(" ")); + last_was_ws = true; + continue; } PatternToken::Passthrough(text) => { tokens.push(Span::plain(text)); } } + last_was_ws = false; } // Remove trailing whitespace loop { diff --git a/core/tests/spans.rs b/core/tests/spans.rs index fb4803b..4f900df 100644 --- a/core/tests/spans.rs +++ b/core/tests/spans.rs @@ -41,3 +41,10 @@ fn correct_whitespace() { test("m s", "1 meter second"); test("kg m / s", "1 kilogram meter / second (impulse)"); } + +#[test] +fn correct_reciprocal_units() { + test("1 mpg", "approx. 425143.7 / meter^2 (fuel_efficiency)"); + test("1 Hz", "1 / second (frequency)"); + test("1 GHz", "1.0e9 / second (frequency)"); +}