Jump to content




string.format("%.3f") not formatting float object


3 replies to this topic

#1 rockymc

  • Members
  • 103 posts

Posted 25 May 2015 - 09:21 PM

I was messing with a program I made, when I realized that when I tried to format a float, the float wouldn't format.

Then, I opened the in-game lua and typed this
print(string.format("%.3f", 3.14159))
And the result was 3.14159 instead of 3.141 (3.142 because it rounds the number).

Is it a known issue? What can I do to fix it?

Edited by rockymc, 25 May 2015 - 09:24 PM.


#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 25 May 2015 - 09:38 PM

Maybe, use a string.

#3 Anavrins

  • Members
  • 775 posts

Posted 25 May 2015 - 09:45 PM

It is known and I believe it's an issue with LuaJ,
Here's a workaround
local function round(num, dec)
  local shift = 10^(dec or 2)
  return math.floor(num * shift + 0.5) / shift
end
round(math.pi) -> 3.14
round(math.pi, 4) -> 3.1416


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 25 May 2015 - 09:59 PM

Known issue with LuaJ.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users