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

Annoying double number

Started by unobtanium, 02 July 2013 - 04:53 PM
unobtanium #1
Posted 02 July 2013 - 06:53 PM
Hey

i am having a doulbe number like 14.25, which gets math.ceil()ed and it is 15.0
The ".0" after the 15 does get printed now, which i dont want.
How get i rid of this?

Sincerly
unobtanium
Yevano #2
Posted 02 July 2013 - 07:43 PM
I just typed print(math.ceil(14.25)) in the Lua interpreter and 15 was printed. What context does this happen in?
Kingdaro #3
Posted 02 July 2013 - 07:55 PM
Try using a format string.


print(string.format('%d', someNumber))

Or,


print(('%d'):format(someNumber))

They both do the same thing, just with slightly different syntax.
theoriginalbit #4
Posted 02 July 2013 - 08:24 PM
Try using a format string.

print(string.format('%d', someNumber))
I'd suggest using %i not %d … %d is very likely that in this context it will keep the .0 as 'd' is Double, so use 'i' for Integer…
MysticT #5
Posted 02 July 2013 - 08:56 PM
Try using a format string.

print(string.format('%d', someNumber))
I'd suggest using %i not %d … %d is very likely that in this context it will keep the .0 as 'd' is Double, so use 'i' for Integer…
Nope, "%d" is for integers too. "%f", "%e" and "%g" are for float/double.
This printf reference lists all the formats you can use. string.format uses printf (in the C version), so this should be the same for CC lua.