PDA

View Full Version : roundTo()

coreys
08-02-2007, 05:58 AM
Here's a function for rounding to different places (tenths place, hundredths place, etc etc)

public function roundTo(val, place) {
temp.tokens = val.tokenize(".");
if (temp.tokens[1].substring(place, 1) < 5) temp.decimal = temp.tokens[1].substring(place);
else if (temp.tokens[1].substring(place, 1) >= 5) temp.decimal = temp.tokens[1].substring(0, 1) + 1;
temp.newval = temp.tokens[0] @ "." @ temp.decimal;
return temp.newval
}
Example:

temp.value = 10.56;
temp.newvalue = roundTo(temp.value, 1); //rounds to the tenths place, 2 would round to the hundredths place, 3 to the thousandths place, etc etc
echo(temp.newvalue);
would return "10.6."

Crow
08-02-2007, 11:06 AM
Very nice! I might use that later, thanks a ton :)

coreys
08-02-2007, 11:09 AM
Thank Stan for having me make it. lol

Horrified
08-02-2007, 11:34 AM
Awesome! I can help 5th Graders with there math homework...on Graal!

Purely, in all standards, deserving the name of v6.

Angel_Light
08-12-2007, 06:36 AM
Heh, Graal should be called a Massive Multiplayer Calculator. ;P

zokemon
08-12-2007, 01:21 PM
Useful but this is a much shorter, simpler and overall less cpu-use way to do it:
(also supports more then just 0.1, 0.01, 0.001, etc.)
public function roundTo(val, place) {
return int(val/place + 0.5)*place;
}

Two examples:

myvalue = 53.42;
roundTo(myvalue, 0.1);

myvalue would be 53.4

myvalue = 53.463;
roundTo(myvalue, 0.05);

myvalue would be 53.45

Basically this just takes value and rounds it to the closest "place".

(Didn't see this thread till just now >_<)

coreys
08-12-2007, 10:05 PM
I hate you...;-;

Inverness
08-12-2007, 10:07 PM
I hate you...;-;
Yea, I kind of wanted to say "Way to ruin someone's day" to Zero.

But oh well, now you are stronger.

coreys
08-13-2007, 06:50 AM
lol, nice way of putting it

zokemon
08-13-2007, 07:17 AM
I'm sorry :(

xAndrewx
11-09-2014, 02:53 PM
thank you. I can round up! :)

rep++ @ Zokemon

Crow
11-09-2014, 03:12 PM
thank you. I can round up! :)

rep++ @ Zokemon

This is part of the shared functions package, by the way.

callimuc
11-09-2014, 03:22 PM
This is part of the shared functions package, by the way.

They don't work on mobile servers sadly. But I remember adding a class with all shared functions to Era iPhone, not too sure if it's still around

Crow
11-09-2014, 05:04 PM
They don't work on mobile servers sadly. But I remember adding a class with all shared functions to Era iPhone, not too sure if it's still around

Ah, good point.