This page is no longer updated

We've left it here for reference. Find Out More.

PlanetHITOP

Rouding Numbers.

Rounding numbers with decimals to become whole numbers is something that you may sometimes need to do. Unfortunatly Hitop has no function to enable you to do this, but Jon Anderson has come up with a way to do it.

Assuming that the number you want rounding up is called NOTROUNDED, you can created a new variable (here called ROUNDED) using the following code:

<@SET NAME="ROUNDED"
VALUE="${'${'${'0.${NOTROUNDED:AFTER('.')}*2':EVAL()}.' :BEFORE('.')}+${'${NOTROUNDED}.':BEFORE('.')}':EVAL()}">

This should all be on one line, but is shown here on multiple lines for clarity. This code will work even if you enter a whole number in.

What this does.

This doesn't particularly look very nice, but it should do the job!

First thing that happens is that any numbers after the decimal point in NOTROUNDED are put into a new decimal number, which is then multipled by 2.

We then look at the first part of this number (before the decimal point). If this is greater than 1, then our number should be rounded up (ie NOTROUNDED has a decimal point of .5 or bigger). If it is 0, then the number shouldn't be rounded up.

We then add either 0 or 1 to NOTROUNDED, and discard the decimal value. And hence you have a number that is rounded up or down as appropiate.

Try it and see.

If you want to see it in motion, try this. Just enter a number in the box below and click on 'round it'.

Hitop 0.35 and below: BEWARE!

Planet Hitop usually runs the latest version of Hitoplive, so this bug won't be displayed here, but for those of you hosting a site on an earlier version, there's a rather nasty bug.

Inputting a non-numerical value into the rounding function will produce a segfault, so if you're hosting on an old browser, you should filter for non-numeric values, lest you want internal server errors to be displayed to your users!


top^

Planet Bods NetworkSites in the network