Checks if a string is a proper decimal format. Optionally, a specific number of digits can be checked too.
boolean Core_Valid::decimal( string $str [, integer $places = integer 2 , integer $digits = null ] )
参数列表
参数 类型 描述 默认值 $str
string
Number to check $places
integer
Number of decimal places integer 2 $digits
integer
Number of digits null
boolean
public static function decimal($str, $places = 2, $digits = NULL)
{
if ($digits > 0)
{
// Specific number of digits
$digits = '{' . ((int)$digits) . '}';
}
else
{
// Any number of digits
$digits = '+';
}
// Get the decimal point for the current locale
list ( $decimal ) = array_values(localeconv());
return (bool)preg_match('/^[0-9]' . $digits . preg_quote($decimal) . '[0-9]{' . ((int)$places) . '}$/D', $str);
}