Rounding Numbers

lyteside
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
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
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
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
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
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...

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums