File helper/NumberHelper.class.php
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
<?php
/**
* Number helper
* @package Helper
* @copyright © 2005-2019 PHPBoost
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
* @author Regis VIARRE <crowkait@phpboost.com>
* @version PHPBoost 5.2 - last update: 2016 10 24
* @since PHPBoost 3.0 - 2010 01 24
* @contributor Arnaud GENET <elenwii@phpboost.com>
*/
class NumberHelper
{
/**
* Converts a string to a numeric value.
* @param string $var The value you want to convert.
* @param string $type 'int' if you want to convert to an integer value, 'float' if you want a floating value.
* @return mixed The integer or floating value (according to the type you chose).
*/
public static function numeric($var, $type = 'int')
{
if (is_numeric($var)) //Retourne un nombre
{
if ($type === 'float')
{
return (float)$var; //Nbr virgule flottante.
}
else
{
return (int)$var; //Nombre entier
}
}
else
{
return 0;
}
}
/**
* Rounds a number
* @param mixed $number Number to round
* @param int $dec The number of decilam points
* @return string The rounded number.
*/
public static function round($number, $dec)
{
return trim(number_format($number, $dec, '.', ''));
}
}
?>