Skip to content

fix(shipping): prevent float truncation in create_quote_from_float and zero-pad cents in Display - #3878

Merged
julianocosta89 merged 2 commits into
open-telemetry:mainfrom
bhuvan-somisetty:fix/shipping-quote-float-truncation-and-display
Sep 2, 2026
Merged

fix(shipping): prevent float truncation in create_quote_from_float and zero-pad cents in Display#3878
julianocosta89 merged 2 commits into
open-telemetry:mainfrom
bhuvan-somisetty:fix/shipping-quote-float-truncation-and-display

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown
Contributor

Changes

Fixes #3877

Problem & Root Cause

  1. In src/shipping/src/shipping_service/quote.rs, create_quote_from_float converts f64 using ((value * 100_f64) as u32) % 100. Due to IEEE-754 binary floating-point representation, multiplying common monetary values such as 8.99 * 100.0 produces 898.9999999999999. Directly casting as u32 truncates towards zero to 898, setting cents: 98 ($8.98) instead of cents: 99 ($8.99). This caused shipping quotes to drop 1 cent on quantities like 1 item ($8.99 -> $8.98), 4 items ($35.96 -> $35.95), and 8 items ($71.92 -> $71.91).
  2. fmt::Display for Quote formatted quotes using write!(f, "{}.{}", self.dollars, self.cents), outputting single-digit cents without zero-padding when cents < 10 (e.g. "0.1" for 1 cent and "10.5" for 5 cents), corrupting demo.shipping.cost.total telemetry attributes and span events.

Solution

  1. Computed total cents using (value * 100_f64).round() as u64 before integer division and modulo in create_quote_from_float.
  2. Updated fmt::Display for Quote to format cents with 2-digit zero padding ({}.{:02}).
  3. Updated and expanded unit tests in src/shipping/src/shipping_service/quote.rs to verify accurate conversion and display across edge cases (8.99, 19.99, 35.96, 71.92, 0.01, 10.05, 100.00).

Merge Requirements

  • CHANGELOG.md updated to document the fix
  • Appropriate documentation updates in the docs (not applicable, internal precision fix)
  • Appropriate Helm chart updates (not applicable, no configuration changed)

…d zero-pad cents in Display

Use round() on the scaled total cents before integer division and modulo to avoid IEEE-754 float truncation (e.g. 8.99 * 100.0 yielding 98 cents instead of 99 cents). Also update fmt::Display for Quote to format cents with 2-digit zero-padding ({:02}), preventing single-digit currency strings in span attributes and events.

Fixes open-telemetry#3877

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
@bhuvan-somisetty
bhuvan-somisetty requested a review from a team as a code owner August 23, 2026 11:04
@github-actions

Copy link
Copy Markdown

This PR was marked stale due to lack of activity. It will be closed in 7 days.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes shipping quote precision and formatting issues described in #3877.

Changes:

  • Rounds total cents before integer conversion.
  • Zero-pads displayed cents and expands regression tests.
  • Documents the fix in the changelog.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/shipping/src/shipping_service/quote.rs Corrects conversion, formatting, and tests.
CHANGELOG.md Records the shipping fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@julianocosta89
julianocosta89 added this pull request to the merge queue Sep 2, 2026
Merged via the queue into open-telemetry:main with commit 935104e Sep 2, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[shipping] Floating-point truncation in create_quote_from_float drops 1 cent and Display lacks two-digit cent padding

3 participants