Skip to content

Commit 866f72a

Browse files
committed
Favor decimal notation over scientific notation for floats
e.g. ``` JSON.dump(1746861937.7842371) ``` master: ``` "1.7468619377842371e+9" ``` This branch and older json versions: ``` 1746861937.7842371 ``` In the end it's shorter, and according to `canada.json` benchmark performance is the same.
1 parent 922f24f commit 866f72a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

ext/json/ext/vendor/fpconv.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
340340
}
341341

342342
/* write decimal w/o scientific notation */
343-
if(K < 0 && (K > -7 || exp < 4)) {
343+
if(K < 0 && (K > -7 || exp < 10)) {
344344
int offset = ndigits - absv(K);
345345
/* fp < 1.0 -> write leading zero */
346346
if(offset <= 0) {

test/json/json_generator_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,14 @@ def test_json_generate_float
771771
values = [-1.0, 1.0, 0.0, 12.2, 7.5 / 3.2, 12.0, 100.0, 1000.0]
772772
expecteds = ["-1.0", "1.0", "0.0", "12.2", "2.34375", "12.0", "100.0", "1000.0"]
773773

774+
if RUBY_ENGINE == "jruby"
775+
values << 1746861937.7842371
776+
expecteds << "1.7468619377842371E9"
777+
else
778+
values << 1746861937.7842371
779+
expecteds << "1746861937.7842371"
780+
end
781+
774782
values.zip(expecteds).each do |value, expected|
775783
assert_equal expected, value.to_json
776784
end

0 commit comments

Comments
 (0)