[ruby/json] Favor decimal notation over scientific notation for floats

e.g.
```
JSON.dump(1746861937.7842371)
```

master:
```
"1.https://github.com/ruby/json/commit/746861937784+9"
```

This branch and older json versions:
```
https://github.com/ruby/json/commit/1746861937.7842371
```

In the end it's shorter, and according to `canada.json` benchmark
performance is the same.

https://github.com/ruby/json/commit/866f72a437
This commit is contained in:
Jean Boussier 2025-05-10 11:31:33 +02:00 committed by Hiroshi SHIBATA
parent dc5555d74a
commit af79914002
2 changed files with 9 additions and 1 deletions

View File

@ -340,7 +340,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
}
/* write decimal w/o scientific notation */
if(K < 0 && (K > -7 || exp < 4)) {
if(K < 0 && (K > -7 || exp < 10)) {
int offset = ndigits - absv(K);
/* fp < 1.0 -> write leading zero */
if(offset <= 0) {

View File

@ -771,6 +771,14 @@ class JSONGeneratorTest < Test::Unit::TestCase
values = [-1.0, 1.0, 0.0, 12.2, 7.5 / 3.2, 12.0, 100.0, 1000.0]
expecteds = ["-1.0", "1.0", "0.0", "12.2", "2.34375", "12.0", "100.0", "1000.0"]
if RUBY_ENGINE == "jruby"
values << 1746861937.7842371
expecteds << "1.7468619377842371E9"
else
values << 1746861937.7842371
expecteds << "1746861937.7842371"
end
values.zip(expecteds).each do |value, expected|
assert_equal expected, value.to_json
end