Skip to content

fix: correct scientific notation formatting for %e/%E#908

Merged
stephenamar-db merged 1 commit into
databricks:masterfrom
He-Pin:fix/format-exponent
Jun 9, 2026
Merged

fix: correct scientific notation formatting for %e/%E#908
stephenamar-db merged 1 commit into
databricks:masterfrom
He-Pin:fix/format-exponent

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix DecimalFormat.format exponent calculation: ceil(log10(x))-1floor(log10(x))
  • Fix .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

Input go-jsonnet jrsonnet sjsonnet (fixed) Python
0.001 1.000000e-03 1.000000e-03 1.000000e-03 1.000000e-03
1.0 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
100.0 1.000000e+02 1.000000e+02 1.000000e+02 1.000000e+02
1000.0 10.000000e+02* 1.000000e+03 1.000000e+03 1.000000e+03
910 9.100000e+02 9.100000e+02 9.100000e+02 9.100000e+02

*go-jsonnet has a known bug for 1000.0; our output matches Python (the spec reference) and jrsonnet.

Test plan

  • Updated DecimalFormatTests expected values to include explicit sign
  • Added golden test format_scientific_notation.jsonnet
  • Full test suite passes
  • Cross-verified with go-jsonnet, jrsonnet, and Python

…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.
@He-Pin

He-Pin commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

I tested it with code in python, this pr is right.

@stephenamar-db stephenamar-db merged commit 18dc301 into databricks:master Jun 9, 2026
5 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.

2 participants