fix: correct scientific notation formatting for %e/%E#908
Merged
Conversation
…s of 10
Motivation:
DecimalFormat.format used ceil(log10(x))-1 to compute the exponent, which
produced wrong results for exact powers of 10 (e.g., 1.0, 100.0) and
numbers less than 1. Additionally, the .replace("E","E+") call in
Format.scala corrupted negative exponents to "E+-".
For example, std.format("%e", 0.001) produced "10.000000e+-04" instead
of the correct "1.000000e-03".
Modification:
- Use floor(log10(x)) for correct exponent calculation in DecimalFormat.
- Include explicit sign (+/-) directly in the exponent fragment.
- Remove all .replace("E","E+") calls in Format.scala.
Result:
Scientific notation now matches go-jsonnet output for all normal numbers.
Verified: 0.001→1.000000e-03, 1.0→1.000000e+00, 100.0→1.000000e+02,
910→9.100000e+02.
Contributor
Author
|
I tested it with code in python, this pr is right. |
stephenamar-db
approved these changes
Jun 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DecimalFormat.formatexponent calculation:ceil(log10(x))-1→floor(log10(x)).replace("E","E+")corrupting negative exponents to"E+-"std.format("%e", 0.001)previously produced"10.000000e+-04", now correctly produces"1.000000e-03"Verification against reference implementations
*go-jsonnet has a known bug for 1000.0; our output matches Python (the spec reference) and jrsonnet.
Test plan
DecimalFormatTestsexpected values to include explicit signformat_scientific_notation.jsonnet