• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Release math.Round Function!

Mooosie

- Lua Scripter -
Joined
Aug 2, 2008
Messages
702
Reaction score
27
Location
Sweden
Hello!
Yesterday I was doing a script that was needed to round off numbers to whole numbers. I tried to use math.Round or math.round, they both did'nt work with our tfs version today. So i decided to make the math.round function working.

math.Round rounds off the number to a whole number. For example: 0.3 becomes 0 and 0.6 becomes 1.

Put this at: .../data/lib/050-function.lua
Lua:
function math.Round(number) 
if number >= 0 then
	return math.floor(number+.5) 
else 
	return math.ceil(number-.5) 
	end
end

So here is an example how to use it:
Lua:
function onUse(cid, item, frompos, item2, topos)
	local heal = getCreatureHealth(cid) / 100 * 10
	doCreatureAddHealth(cid, math.Round(heal)) -- If you example had 6253 HP, you would get 625,3 HP. But if you round it, you get 625 HP.
	doSendAnimatedText(getCreaturePosition(cid), "".. math.Round(heal) .."", COLOR_GREEN) -- This is why you need to use this function. You don't want to see 625,3 above you when you heal. Tibia does'nt even use decimals as heal or damage. So it is not necessary to heal or damage 0.3-
	doSendMagicEffect(getCreaturePosition(cid), 12)
	doRemoveItem(item.uid, 1)
return true
end

So it heals whole numbers always.

This is why you need to use this function. You don't want to see 625,3 above you when you heal. Tibia does'nt even use decimals as heal or damage. So it is unnecessary to heal or damage 0.3-

Thanks and enjoy! :)
 
Last edited:
I think he made it because he didn't want it to always round the number down.
 
math.abs() return the absolute value of the number !
 
Back
Top