Rounding help...
#1

How to round a number that the result's last cipher is 5 or 0,

could it be something like that? :
Код:
new rounded, num;
num = 47;
rounded = (num / 5) * 5;
in my example, 'rounded' should be 45..
Reply
#2

Quote:
Originally Posted by lesley
How to round a number that the result's last cipher is 5 or 0,

could it be something like that? :
Код:
new rounded, num;
num = 47;
rounded = (num / 5) * 5;
in my example, 'rounded' should be 45..
Here's the newbish way to do it. I'm sure someone will come along and find a better way to do it sooner or later.
pawn Код:
new num1, num2;
num1 = 4;
num2 = 7;
if(num2 < 5) { num2 = 0; } else if(num2 >= 5) { num2 = 5; }
Reply
#3

Quote:
Originally Posted by [Fackin'
Pyro ]
Quote:
Originally Posted by lesley
How to round a number that the result's last cipher is 5 or 0,

could it be something like that? :
Код:
new rounded, num;
num = 47;
rounded = (num / 5) * 5;
in my example, 'rounded' should be 45..
Here's the newbish way to do it. I'm sure someone will come along and find a better way to do it sooner or later.
pawn Код:
new num1, num2;
num1 = 4;
num2 = 7;
if(num2 < 5) { num2 = 0; } else if(num2 >= 5) { num2 = 5; }
Yeah that isnt a good way because i want to define my number via --> 1 <-- simple integer variable, not 2 or so..

That code I posted in my first post worked so far, but it only rounds down, for example: it will round 49 to 45, which should be 50..
Reply
#4

pawn Код:
new rounded, num;
num = 46;
if((num % 5) > 2) num += 5;
rounded = num / 5 * 5;
Or

pawn Код:
new rounded, num;
num = 46;
rounded = floatround(floatdiv(num, 5.0), floatround_round) * 5;
Reply
#5

Quote:
Originally Posted by ♣ ⓐⓢⓢ
pawn Код:
new rounded, num;
num = 46;
if((num % 5) > 2) num += 5;
rounded = num / 5 * 5;
Or

pawn Код:
new rounded, num;
num = 46;
rounded = floatround(floatdiv(num, 5.0), floatround_round) * 5;
Great! Thanks! Btw, what is the operator or w/e '%' for? e.g. if((num % 5) > 2) num += 5;
Reply
#6

note that:
pawn Код:
rounded = num / 5 * 5;
gives another result as ;
pawn Код:
rounded = floatround(floatdiv(num, 5.0), floatround_round) * 5;
trust me, i have done this before, this will give another result; the float way is more accurate
Reply
#7

Quote:
Originally Posted by maij
note that:
pawn Код:
rounded = num / 5 * 5;
gives another result as ;
pawn Код:
rounded = floatround(floatdiv(num, 5.0), floatround_round) * 5;
trust me, i have done this before, this will give another result; the float way is more accurate
Yeah now i understand these functions, thanks!
Reply
#8

Quote:
Originally Posted by maij
note that:
pawn Код:
rounded = num / 5 * 5;
gives another result as ;
pawn Код:
rounded = floatround(floatdiv(num, 5.0), floatround_round) * 5;
trust me, i have done this before, this will give another result; the float way is more accurate
for the first you need the if statment to let it work

Quote:
Originally Posted by lesley
Great! Thanks! Btw, what is the operator or w/e '%' for? e.g. if((num % 5) > 2) num += 5;
It divides oper1 through oper2 and return the rest
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)