[SOLVED] Round

hi all,

probably a stupid question…

how do i:

12.3456778

round into

12.35?

the math.round doesn’t do the trick.

and i dont want to do * 100, round, and devide again (1234.56778, round, 12.35)… or is this the only way?

thanks! [import]uid: 90610 topic_id: 27401 reply_id: 327401[/import]

You should start here: http://lua-users.org/wiki/StringsTutorial

It doesn’t give an exact case but something close, I think. [import]uid: 8271 topic_id: 27401 reply_id: 111306[/import]

Ok, this code does what you want:

[lua]print( string.format("%.2f", 12.3456778) )[/lua]

The ‘2’ in the first argument of the format function sets the number of characters after the ‘.’

And you can try it here:

http://www.lua.org/cgi-bin/demo [import]uid: 8271 topic_id: 27401 reply_id: 111333[/import]

@horacebury: thanks mate! I didn’t think about a string function, since i am dealing with floats… [import]uid: 90610 topic_id: 27401 reply_id: 111334[/import]

Try this, I think it’ll give you what you’re ultimately looking for:

[lua]local a = 12.3456778
local b = string.format("%.2f", a)
local c = tonumber( b )
local d = c + 1.0
print(d)[/lua]

Basically, takes your original number, trims the decimal places, then continues using it as a number in numeric calculations. [import]uid: 8271 topic_id: 27401 reply_id: 111337[/import]