New math function submission: round_to()

From: Date: Thu, 14 Sep 2000 07:30:46 +0000
Subject: New math function submission: round_to()
Groups: php.dev 
Request: Send a blank email to php-dev+get-33220@lists.php.net to get a copy of this message
Following a question on the php-general list, I hacked this up. It takes two numbers and returns the first number rounded to the nearest integer multiple of the absolute value of the second. For instance, round_to(1120, 50) returns 1100, round_to(156, 50) returns 150, etc. Any comments? Index: ext/standard/basic_functions.c =================================================================== RCS file: /repository/php4/ext/standard/basic_functions.c,v retrieving revision 1.258 diff -u -r1.258 basic_functions.c --- ext/standard/basic_functions.c 2000/09/12 20:48:33 1.258 +++ ext/standard/basic_functions.c 2000/09/14 07:23:59 @@ -275,6 +275,7 @@ PHP_FE(number_format, NULL) PHP_FE(ip2long, NULL) PHP_FE(long2ip, NULL) + PHP_FE(round_to, NULL) PHP_FE(getenv, NULL) #ifdef HAVE_PUTENV Index: ext/standard/math.c =================================================================== RCS file: /repository/php4/ext/standard/math.c,v retrieving revision 1.35 diff -u -r1.35 math.c --- ext/standard/math.c 2000/07/30 21:55:27 1.35 +++ ext/standard/math.c 2000/09/14 07:23:59 @@ -100,10 +100,32 @@ } /* }}} */ +/* {{{ proto int round_to(int number, int multiple) + Returns number rounded to the nearest multiple */ +PHP_FUNCTION(round_to) { + zval **number, **multiple; + int mod; + + if (ZEND_NUM_ARGS() != 2 || + zend_get_parameters_ex(ZEND_NUM_ARGS(), &number, &multiple) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long_ex(number); + convert_to_long_ex(multiple); + + mod = (*number)->value.lval % (*multiple)->value.lval; + + RETURN_LONG(mod >= (int) ((*multiple)->value.lval / 2) ? + (*number)->value.lval + ((*multiple)->value.lval - mod) : + (*number)->value.lval - mod); +} + +/* }}} */ /* {{{ proto double round(double number [, int precision]) Returns the number rounded to specified precision. */ + PHP_FUNCTION(round) { zval **value, **precision; Index: ext/standard/php_math.h =================================================================== RCS file: /repository/php4/ext/standard/php_math.h,v retrieving revision 1.5 diff -u -r1.5 php_math.h --- ext/standard/php_math.h 2000/08/19 22:01:37 1.5 +++ ext/standard/php_math.h 2000/09/14 07:23:59 @@ -54,6 +54,7 @@ PHP_FUNCTION(number_format); PHP_FUNCTION(deg2rad); PHP_FUNCTION(rad2deg); +PHP_FUNCTION(round_to); #include <math.h> -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.dev (#33220) next »