Rounding

Share your scripts and packages with other Mudlet users.
Post Reply
naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Rounding

Post by naftali »

I found myself wanting to round to numbers of greater accuracy than whole numbers (4.325 rounding to 4.33 instead of 4) so I wrote a pair of functions to do that!

Code: Select all

function roundUp(number, places)
	return math.ceil(number * math.pow(10,places)) / math.pow(10,places)
end

function roundDown(number, places)
	return math.floor(number * math.pow(10,places)) / math.pow(10,places)
end
Not sure if there's a way to make these faster using something other than math.pow, but I couldn't think of an easy way to code it. Enjoy!

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: Rounding

Post by hempa »

Personally, if I want a number rounded and printed, I use string.format("%0.2f",number) to get it nicely rounded to two decimals. That can't do round up/down though, I think.

Post Reply