Skip to content

Instantly share code, notes, and snippets.

@actboy168
Created February 21, 2019 07:12
Show Gist options
  • Save actboy168/34cd73d0f99e76c8cd205be80e7f67c7 to your computer and use it in GitHub Desktop.
Save actboy168/34cd73d0f99e76c8cd205be80e7f67c7 to your computer and use it in GitHub Desktop.
local format = string.format
local tonumber = tonumber
local fmt = {
'%.1f',
'%.2f',
'%.3f',
'%.4f',
'%.4f',
'%.5f',
'%.6f',
'%.7f',
'%.8f',
'%.9f',
'%.10f',
'%.11f',
'%.12f',
'%.13f',
'%.14f',
'%.15f',
'%.16f',
}
local function convertreal(v)
for i = 1, #fmt do
local g = format(fmt[i], v)
if tonumber(g) == v then
return g
end
end
return format('%.17f', v)
end
local function convertreal2(v)
local g = format('%.16g', v)
if tonumber(g) == v then
return g
end
return format('%.17g', v)
end
local function test(s1)
local f1 = tonumber(s1)
local s2 = convertreal(f1)
print(s1)
print(s2)
print('-------------------')
end
test '0.1'
test '0.000001'
test '0.00000001'
test '0.0000000001'
test '0.000000000001'
test '0.00000000000001'
test '0.0000000000000001'
test '1.012345678901'
test '1.0123456789012'
test '1.01234567890123'
test '1.012345678901234'
test '1.0123456789012345'
test '1.01234567890123456'
test '1.012345678901234567'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment