Source for file Numbers.class.php
Documentation is available at Numbers.class.php
* @var array Contains all hexadecimal numbers.
require_once CLASSPATH. "data_structures/ABSTObject.class.php";
* Static methods to handle numeric values.
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* Apportions a range of numbers between zero and $rangeEnd in $portions
* commensurate chunks. Then the closest computed value to $value will
* Numbers::getIntervalRoundingOf( 42, 60, 4 );
* if( $value <= 7 || $value > 52 )
* if( $value <= 22 && $value > 7 )
* if( $value <= 37 && $value > 22 )
* if( $value <= 52 && $value > 37 )
* In the upper example the function will return 45.
* @author Daniel Plücken <daniel@debakel.net>
* @param number $value The value to round to the nearest chunk of the
* @param number $rangeEnd The end of the range. The range begins at zero.
* For example if you want to round in the range of
* 6 to 9, then set the $rangeEnd to 3 and add 6
* @param number $portions How many chunks should create in the range for
$intervalStepAmount = $rangeEnd / $portions;
$halfIntervalStepAmount = $intervalStepAmount / 2;
$fa = floor($halfIntervalStepAmount);
$ca = ceil($halfIntervalStepAmount);
{ # Rounding like a merchant (e.g. rounding up if the value is 0.5
if ( $value <= $fa || $value > ($rangeEnd - $ca) )
for ( $i = 1; $i < $portions; $i++ )
$value <= ( ($i * $intervalStepAmount) + $fa )
&& $value > ( ($i * $intervalStepAmount) - $ca )
return ($i * $intervalStepAmount);
* Returns a random number in the range between $including_from and
* $including_to. Sets automatically a new random set by microtime for real
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $including_from
* @param integer $including_to
function random( $including_from, $including_to )
return rand( $including_from, $including_to );
* Returns a double from a given string. Translates a number from other
* @author Daniel Plücken <daniel@debakel.net>
* @param string $str_number
* @param string $str_decimal_separator
* @param string $str_thousand_separator
$str_decimal_separator = ".",
$str_thousand_separator = ","
$str_out = str_replace( $str_thousand_separator, "", $str_number );
$str_out = str_replace( $str_decimal_separator, ".", $str_out );
* Returns the decimal value of a given hexadecimal value.
* @author Daniel Plücken <daniel@debakel.net>
for( $i = 0; $i < strlen( $string ); $i++ )
switch( substr( $string, $i, 1 ) )
$out += $tmp * pow( 16, (strlen( $string )- 1) - $i );
} // End of class Numbers
|