ruby / ruby/json

Output formatting differences between jRuby 10.0.0.1 and Ruby 3.4 when converting Float to JSON.

Open
#817 3 comments 1 reaction 1 assignee View on GitHub

@headius is already working on this.

Since Aug 25, 2025.

jruby
Dominant language
Ruby
Stars
782
Forks
383
Avg merge
1d 18h
Merged PRs (30d)
10

Description

In Ruby 3.4, when converting a Float value to JSON using .to_json, the output notation is more or less preserved. Namely, if your value is in scientific notation, the output will be in scientific notation. If it's decimal, it will remain decimal, though there does appear to be a precision limit where it will truncate additional decimals.

In jRuby 10.0.0.1, Float values above a certain size will always be converted to scientific notation in the output. The precision doesn't appear to be the culprit, it seems to be the number of digits to the LEFT of the decimal that determines this. If there are more than 7, it will convert the notation.

For example (in jRuby 10):

irb(main):001> x = 1234567.1234567893
=> 1234567.1234567894
irb(main):002> x.to_json
=> "1234567.1234567894"
irb(main):003> x = 12345678.12345678912
=> 12345678.12345679
irb(main):004> x.to_json
=> "1.234567812345679E7"

Versus (in Ruby 3.4.1):

irb(main):001> x = 1234567.1234567893
=> 1234567.1234567894
irb(main):002> x.to_json
=> "1234567.1234567894"
irb(main):003> x = 12345678.12345678912
=> 12345678.12345679
irb(main):004> x.to_json
=> "12345678.12345679"

In particular, this causes issues with JSON output containing timestamps in epoch+milliseconds output, such as what you would get from Time.now.to_f.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.