当0.00是可接受的值时,检查执行字符串到浮点转换的错误
I am currently looking for the most elegant way, to do error-checking on a string to float conversion, when 0.00 shall be an accepted value, but non-numeric characters shall lead to a bailout. Currently I am thinking of testing the string for any other chars than ","/"." or "[0-9]". I`d just like to know, if there is a more elegant/shorter way. Thanks in advance, Oliver !
我目前正在寻找最优雅的方法,对字符串进行错误检查以进行浮动转换,当0.00时 应该是一个可接受的值,但是非数字字符应该导致救助。 目前我正在考虑测试字符串中除“,”/“之外的任何其他字符。” 或“[0-9]”。 我想知道,如果有更优雅/更短的方式。 谢谢提前,奥利弗! p> div>
PHP has a built in function for this.
is_float()
Examples:
is_float(27.25); // This would be true
is_float('abc'); // This would be false
I think you're looking for is_numeric()
http://php.net/manual/en/function.is-numeric.php
$x = 5.345;
$y = '0.00';
$z = 'abc123';
echo is_numeric($x)."1
";
echo is_numeric($y)."2
";
echo is_numeric($z)."3
";
See it working here https://3v4l.org/sEvqC