Jump to content




Annoying double number


  • You cannot reply to this topic
4 replies to this topic

#1 unobtanium

  • Members
  • 505 posts

Posted 02 July 2013 - 05: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

#2 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 02 July 2013 - 06:43 PM

I just typed print(math.ceil(14.25)) in the Lua interpreter and 15 was printed. What context does this happen in?

#3 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 02 July 2013 - 06: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.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 July 2013 - 07:24 PM

View PostKingdaro, on 02 July 2013 - 06:55 PM, said:

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...

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 02 July 2013 - 07:56 PM

View Posttheoriginalbit, on 02 July 2013 - 07:24 PM, said:

View PostKingdaro, on 02 July 2013 - 06:55 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users