Skip to content

Fix Number.prototype.toExponential() rounding (closes #4169)#4998

Closed
xcb3d wants to merge 1 commit intoboa-dev:mainfrom
xcb3d:fix/toExponential-rounding
Closed

Fix Number.prototype.toExponential() rounding (closes #4169)#4998
xcb3d wants to merge 1 commit intoboa-dev:mainfrom
xcb3d:fix/toExponential-rounding

Conversation

@xcb3d
Copy link
Contributor

@xcb3d xcb3d commented Mar 11, 2026

This Pull Request fixes/closes #4169.

It changes the following:

  • Replaces Rust's format!("{n:.prec$e}") in f64_to_exponential_with_precision which uses banker's rounding (round-half-to-even) with a manual implementation that uses round-half-away-from-zero to match ECMAScript semantics
  • The new implementation extracts full-precision digits via format!("{n:.100e}"), then rounds the digit string correctly before formatting the exponential output
  • Fixes (25).toExponential(0) returning "2e+1" instead of "3e+1" and (12345).toExponential(3) returning "1.234e+4" instead of "1.235e+4"
  • The test262 return-values.js now passes in both normal and strict mode

@xcb3d xcb3d requested a review from a team as a code owner March 11, 2026 13:44
@github-actions
Copy link

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 49,901 49,902 +1
Ignored 2,222 2,222 0
Failed 840 839 -1
Panics 0 0 0
Conformance 94.22% 94.22% +0.00%
Fixed tests (1):
test/built-ins/Number/prototype/toExponential/return-values.js (previously Failed)

Tested main commit: a49348586d90b790936dc6ab651da6aff3f60e67
Tested PR commit: bdc6c35b78e4ba2ae58188d373ce1e36daaf5f2c
Compare commits: a493485...bdc6c35

@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 0% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.38%. Comparing base (6ddc2b4) to head (bdc6c35).
⚠️ Report is 806 commits behind head on main.

Files with missing lines Patch % Lines
core/engine/src/builtins/number/mod.rs 0.00% 35 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4998       +/-   ##
===========================================
+ Coverage   47.24%   58.38%   +11.14%     
===========================================
  Files         476      559       +83     
  Lines       46892    61435    +14543     
===========================================
+ Hits        22154    35871    +13717     
- Misses      24738    25564      +826     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jedel1043
Copy link
Member

jedel1043 commented Mar 11, 2026

The issue specifically states avoiding hacks using format!. We have a WIP implementation for this on ryu-js: boa-dev/ryu-js#50

@jedel1043 jedel1043 closed this Mar 11, 2026
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.

Fix Number.prototype.toExponential() value rounding

2 participants