Raised This Month: $ Target: $400
 0% 

PowerOfFive


Post New Thread Reply   
 
Thread Tools Display Modes
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-23-2014 , 07:12   Re: PowerOfFive
Reply With Quote #11

This one is untested, but SHOULD let you specify the multiple you want to round to. (in your case, 5) Probably only woks with positive numbers.

PHP Code:
stock RoundToMultiple(inputmultipleroundup=true) {
    new 
remainder input multiple;
    if (
remainder == 0) {
        return 
input;
    }
    new 
double remainder 2;    
    if (
double multiple || (double == multiple && !roundup)) {
        return (
input-remainder);
    }
    else {
        return (
input+(multiple-remainder));
    }

__________________

Last edited by ddhoward; 02-23-2014 at 07:25.
ddhoward is offline
Pelipoika
Veteran Member
Join Date: May 2012
Location: Inside
Old 02-23-2014 , 07:14   Re: PowerOfFive
Reply With Quote #12

Quote:
Originally Posted by ddhoward View Post
It would be easier to use the modulo function, methinks.

PHP Code:
stock RoundToFive(input) {
    new 
remainder input 5;
    if (
remainder == 0) return input;
    if (
remainder <= 2) return (input-remainder);
    if (
remainder >= 3) return (input+(5-remainder));

Perfect! Thank you so much.

Cookies.net's solution also worked but it doesnt convert the numbers down (Not that big of a deal for my purposes tho but ill use ddHowards stock)
Pelipoika is offline
Cookies.net
Senior Member
Join Date: Jan 2011
Old 02-23-2014 , 07:26   Re: PowerOfFive
Reply With Quote #13

Quote:
Originally Posted by Pelipoika View Post
Cookies.net's solution also worked but it doesnt convert the numbers down (Not that big of a deal for my purposes tho but ill use ddHowards stock)
Ah, my mistake, it should have been
PHP Code:
//For integers
new output = ((input 2) / 5) * 5
The 2 comes from an integer divison (5/2=2)

for any number it would be something like this
PHP Code:
//For integers
new output = ((input + (nth 2)) / nth) * nth
Cookies.net is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-23-2014 , 07:27   Re: PowerOfFive
Reply With Quote #14

Updated the above stock, the third optional argument (defaults to true) sets whether the function should round up or down if the input is directly in the middle of two multiples. (for example, 15 being rounded to 10 or 20?)
__________________
ddhoward is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:35.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode