Skip to content

Commit

Permalink
fix(encode_number): support up to 20 significant digits when encoding…
Browse files Browse the repository at this point in the history
… decmials, return string value of integers

As mentioned in rxi#42 (comment) - Lua 5.3+ supports 64 bit integers. This change modifies the string encoding of numbers to align with those changes.
  • Loading branch information
dtfiedler authored Nov 5, 2024
1 parent dbf4b2d commit 2885270
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ local function encode_number(val)
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
-- Handle integer values separately to avoid floating-point conversion
if math.type(val) == "integer" then
return string.format("%d", val) -- Format as an integer
else
-- Use 20 significant digits for non-integer numbers
return string.format("%.20g", val)
end


Expand Down

0 comments on commit 2885270

Please sign in to comment.