Rounding help... - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Rounding help... (
/showthread.php?tid=68801)
Rounding help... -
lesley - 13.03.2009
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..
Re: Rounding help... -
Pyrokid - 13.03.2009
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; }
Re: Rounding help... -
lesley - 13.03.2009
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..
Re: Rounding help... -
Nero_3D - 13.03.2009
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;
Re: Rounding help... -
lesley - 14.03.2009
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;
Re: Rounding help... -
maij - 14.03.2009
note that:
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
Re: Rounding help... -
lesley - 14.03.2009
Quote:
|
Originally Posted by maij
note that: 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!
Re: Rounding help... -
Nero_3D - 14.03.2009
Quote:
|
Originally Posted by maij
note that: 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