Skip to content

Commit

Permalink
fix(json): modify to 20 significant digits and check if integer directly
Browse files Browse the repository at this point in the history
Lua 5.3 supports 64 bit integers, which shouldn't need to be converted to strings. Floating point numbers will be rounded to 20 sig digits will be rounded.
  • Loading branch information
dtfiedler authored Oct 23, 2024
1 parent 0acefd8 commit 2c8aa83
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions dev-cli/container/src/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ end
local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
error("unexpected number value '" .. tostring(val) .. "'")
end
-- 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
if math.floor(val) == val then
-- Large integers: avoid scientific notation and print as an integer
return string.format("%.0f", val)
else
-- Decimals: use the 'g' format to print floating point with precision, up to 14 significant digits
return string.format("%.14g", val)
end
end

local type_func_map = {
Expand Down

0 comments on commit 2c8aa83

Please sign in to comment.