This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Namarius's profile picture

[1.41][SSP/BUKKIT]string.format doesn't understand format placeholders

Started by Namarius, 10 September 2012 - 07:51 PM
Namarius #1
Posted 10 September 2012 - 09:51 PM
ComputerCraft Version: 1.41 Client / 1.41 Bukkit Server

Description:

All Floating-point format placeholders for string.format doesn't work as expected. Lua documentation stades that all format placeholders should behave like standard C formatted print functions. ComputerCraft's Lua seems to ignore width and precision information only for floating-point.

I've created a little test program for this:

file=io.open("output","w")
function fprint(s)
	print(s)
file:write(s.."\n")
end
fprint("Standard")
fprint(string.format("'%f'",math.pi))
fprint(string.format("'%d'",math.pi))
fprint(string.format("'%e'",math.pi))
fprint(string.format("'%E'",math.pi))
fprint(string.format("'%0x'",0))
fprint(string.format("'%x'",math.pi*1000))
fprint(string.format("'%X'",math.pi*1000))
fprint(string.format("'%o'",math.pi*1000))
fprint("Simple Format")
fprint(string.format("'%7f'",math.pi))
fprint(string.format("'%7d'",math.pi))
fprint(string.format("'%7e'",math.pi))
fprint(string.format("'%7E'",math.pi))
fprint(string.format("'%7x'",math.pi*1000))
fprint(string.format("'%7X'",math.pi*1000))
fprint(string.format("'%7o'",math.pi*1000))
fprint("Advanced Format")
fprint(string.format("'%7.2f'",math.pi))
fprint(string.format("'%7.2e'",math.pi))
fprint(string.format("'%7.2E'",math.pi))
file:close()

and here it's output:

Standard
'3.141592653589793'
'3'
'3.141592653589793'
'3.141592653589793'
'0'
'c45'
'C45'
'6105'
Simple Format
'3.141592653589793'
'	  3'
'3.141592653589793'
'3.141592653589793'
'	c45'
'	C45'
'   6105'
Advanced Format
'3.141592653589793'
'3.141592653589793'
'3.141592653589793'

and this is the expected output tested with online Lua demo (fprint replaced with print and without io.*):

Standard
'3.141593'
'3'
'3.141593e+00'
'3.141593E+00'
'0'
'c45'
'C45'
'6105'
Simple Format
'3.141593'
'	  3'
'3.141593e+00'
'3.141593E+00'
'	c45'
'	C45'
'   6105'
Advanced Format
'   3.14'
'3.14e+00'
'3.14E+00'
Cloudy #2
Posted 10 September 2012 - 10:00 PM
ComputerCraft Version: 1.41 Client / 1.41 Bukkit Server

Description:

All Floating-point format placeholders for string.format doesn't work as expected. Lua documentation stades that all format placeholders should behave like standard C formatted print functions. ComputerCraft's Lua seems to ignore width and precision information only for floating-point.

I've created a little test program for this:

file=io.open("output","w")
function fprint(s)
	print(s)
file:write(s.."n")
end
fprint("Standard")
fprint(string.format("'%f'",math.pi))
fprint(string.format("'%d'",math.pi))
fprint(string.format("'%e'",math.pi))
fprint(string.format("'%E'",math.pi))
fprint(string.format("'%0x'",0))
fprint(string.format("'%x'",math.pi*1000))
fprint(string.format("'%X'",math.pi*1000))
fprint(string.format("'%o'",math.pi*1000))
fprint("Simple Format")
fprint(string.format("'%7f'",math.pi))
fprint(string.format("'%7d'",math.pi))
fprint(string.format("'%7e'",math.pi))
fprint(string.format("'%7E'",math.pi))
fprint(string.format("'%7x'",math.pi*1000))
fprint(string.format("'%7X'",math.pi*1000))
fprint(string.format("'%7o'",math.pi*1000))
fprint("Advanced Format")
fprint(string.format("'%7.2f'",math.pi))
fprint(string.format("'%7.2e'",math.pi))
fprint(string.format("'%7.2E'",math.pi))
file:close()

and here it's output:

Standard
'3.141592653589793'
'3'
'3.141592653589793'
'3.141592653589793'
'0'
'c45'
'C45'
'6105'
Simple Format
'3.141592653589793'
'	  3'
'3.141592653589793'
'3.141592653589793'
'	c45'
'	C45'
'   6105'
Advanced Format
'3.141592653589793'
'3.141592653589793'
'3.141592653589793'

and this is the expected output tested with online Lua demo (fprint replaced with print and without io.*):

Standard
'3.141593'
'3'
'3.141593e+00'
'3.141593E+00'
'0'
'c45'
'C45'
'6105'
Simple Format
'3.141593'
'	  3'
'3.141593e+00'
'3.141593E+00'
'	c45'
'	C45'
'   6105'
Advanced Format
'   3.14'
'3.14e+00'
'3.14E+00'

Not really anything we can do about that without altering LuaJ's internals - and we aren't going to do that. We will be moving away from LuaJ in a future version though.
Namarius #3
Posted 11 September 2012 - 10:02 PM
Okay, good to know.