Rounding up numbers

Hello,

as I'm currently working on a converter between american and european height measurements, I would need a round function aswell. I thought of simply using RTrim to trim the numbers as strings, but I thought there could have been some better alternative there.

So far, I have the following function in Quest:

<function name="HeightConversion" parameters="cmvalue" type="int">
inchvalue = cmvalue * 0.3937
feetvalue = inchvalue * 0.08333333
msg (inchvalue)
msg (feetvalue)
</function>


So my next steps would be:

Cutting the feetvalue to a single digit number (integer) and multiply the result by 12 to get the inch-value.
Substract that inch-value from the variable "inchvalue".
That should give me the height as feetvalue' inchvalue".

I myself am european and use the metric system, but I would like to make something similar to an engine for RPG-elements, and being able to show values such as height and weight in both forms seems like a good idea.

This is the function that Alex originally posted:


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


Quest doesn't use that specific syntax to declare functions anymore, but you should be able to follow the logic and write that function with the current version of Quest. If you have any specific questions (or just don't know where to start) ask away.

Note that you'll need these functions:

http://quest5.net/wiki/If

http://quest5.net/wiki/Instr

http://quest5.net/wiki/Left

http://quest5.net/wiki/Mid

http://quest5.net/wiki/Return

http://quest5.net/wiki/Set (read the note about when you need set versus declaring attributes explicitly)

You have three rounding functions:

floor - rounds down to the next lowest integer
ceiling - rounds up to the next highest integer
round - rounds up or down depending on whether the fractional part is greater or equal to 0.5.

      x = 3.14
msg(floor(x))
msg(ceiling(x))
msg(round(x))

3
4
3

     x = 3.6
msg(floor(x))
msg(ceiling(x))
msg(round(x))

3
4
4
      x = -3.14
msg(floor(x))
msg(ceiling(x))
msg(round(x))

-4
-3
-3
      x = -3.6
msg(floor(x))
msg(ceiling(x))
msg(round(x))

-4
-3
-4

Hope that helps.

If you want to simply discard the fractional part, you can also do this:

x = 3.14
msg(cast(x, int))

3

x = -3.14
msg(cast(x, int))

-3

@jay, that's funny, I looked on the wiki for Floor, ceiling, etc. but didn't see it. Are they there? Is the documentation somewhere where someone could post it to the wiki?

That makes me wonder if there are other functions not on the wiki?

The expression parser used in Quest is FLEE. The docs are here:

https://flee.codeplex.com/

But even those docs don't list floor and ceiling. I believe it exposes standard C# functions.

Here's a forum post I had done after doing some experimentation:

viewtopic.php?f=18&t=3595

indexes!! Holy toledo! :D

edit: hmm, so I'm not sure -- if we use FLEE functions, are they not converted to JS if we turn our games into mobile apps?

That I don't know, to be honest. Alex would know better about specific functions.

Quest imports the .net Math functions: http://msdn.microsoft.com/en-us/library ... .math.aspx

Most of these are available in JavaScript so no problems there.

I would avoid using "cast" though, as that's a FLEE function and won't be available in JavaScript.

I had a play around with these, and write a Wiki page about it:

http://quest5.net/wiki/Use_maths_functi ... Operations

That's awesome Pixie.

I've already started my geometrical RPG btw...