Rounding Numbers

lyteside
28 Mar 2007, 22:42
In Quest 3, the numbers automatically rounded to the nearest whole number, but now it will devide in in half if its closer... like 2.5 is there any way to change this?

right now, i'm unable to process complicated percentage procedures and output whole integers which i need.

Elexxorine
29 Mar 2007, 09:54
This should be what's done, dunno if it's even on the list to add. There should be a function $round(%numbertoround%; %numberofdecimalplaces%)$. Where zero in the second paramter wold give a whole number. You'll have to ask Alex though.

Alex
29 Mar 2007, 10:27
Here's a function that rounds to the nearest whole number:


define function <round>
set string <round.input; $parameter(1)$>
set string <round.dotpos; $instr(#round.input#;.)$>
if ( #round.dotpos# = 0 ) then return <#round.input#>
else {
set numeric <round.leftchars; #round.dotpos# - 1>
set numeric <round.midchars; #round.dotpos# + 1>
set numeric <round.left; $left(#round.input#; %round.leftchars%)$>
set string <round.mid; $mid(#round.input#; %round.midchars%)$>
set numeric <round.up; 0.#round.mid#>
if ( %round.up% >= 0.5 ) then inc <round.left>
return <%round.left%>
}
end define


Examples:

$round(1.4)$ returns 1
$round(1.5)$ returns 2
$round(1.6)$ returns 2
$round(1234.567)$ returns 1235

Elexxorine
29 Mar 2007, 12:24
Cool. lol. If you did some nifty coding you could expand that to round to any number of decimals places (need a for / loop most likely though). I I'm feeling really bored and stuck on my game I'll have a look at expanding it for you Alex...

Alex
29 Mar 2007, 13:32
One thing I've realised is that it doesn't round negative numbers correctly, so that's something else that could be expanded upon.

Elexxorine
30 Mar 2007, 09:13
Yes, that code would round them backwards, you'd only need one extra line to check if it's negative and do the reserve of whatever you were about to do...