Rounding up numbers

SquirrelUK
19 May 2010, 12:33
Hi,

I'm designing a game where the programming includes a bit of mathematics to produce certain numeric variables. How do I make Quest round up a number, so I don't end up irritating my players by filling their screens with numbers like "66.666666666666666666666666666..."? Is there a way I can make the variable only return a whole number, or say, display a number to only 2 decimal points?

Any help much appreciated.

Alex
19 May 2010, 17:49
Here is an example of a "round" function which you can put into your game. You can call it like this:


$round(%health%; 5)$


That would round your "health" variable to 5 decimal places.

Here is the function in a demo ASL file:


define game <Rounding>
asl-version <410>
start <room>
end define

define room <room>
command <round> {
msg <Enter the number you want to round:>
set numeric <number; $enternumber$>
msg <How many decimal places?>
set numeric <dp; $enternumber$>
msg <Rounding %number% to %dp% decimal places: $round(%number%; %dp%)$>
}
end define

define text <intro>
Type ROUND to test rounding.
end define

define function <enternumber>
set numeric <value; 0>
set string <input;>
repeat until (%value% = #input#) {
enter <input>
msg <- #input#>
set numeric <value; #input#>
if (%value% <> #input#) then msg <That is not a valid number! Please enter a valid number:>
}
return <%value%>
end define

define function <round>
set numeric <round.number; $parameter(1)$>
set numeric <round.dp; $parameter(2)$>
set numeric <round.decimalpos; $instr(%round.number%; .)$>
if (%round.decimalpos% = 0) then {
return <%round.number%>
}
else {
set string <round.beforedp; $left(%round.number%; {%round.decimalpos%-1})$>
set string <round.afterdp; $mid(%round.number%; {%round.decimalpos%+1})$>
set string <round.afterdp; $left(#round.afterdp#; %round.dp%)$>
return <#round.beforedp#.#round.afterdp#>
}
end define

SquirrelUK
21 May 2010, 17:25
Thanks for your help again, Alex!

SquirrelUK
27 Apr 2011, 20:47
Greetings,

Sorry to trouble you again on this. I've been trying the rounding fuction on the new version of the program and it isn't working...any advice?

SquirrelUK

Alex
27 Apr 2011, 23:18
In what way is it not working?