php 检查数字是否为十进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6772603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if number is decimal
提问by CodeVirtuoso
I need to check in PHP if user entered a decimal number (US way, with decimal point: X.XXX)
如果用户输入了十进制数,我需要检查 PHP(美国方式,小数点:X.XXX)
Any reliable way to do this?
任何可靠的方法来做到这一点?
回答by cwallenpoole
You can get most of what you want from is_float, but if you reallyneed to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):
你可以从is_float得到你想要的大部分内容,但如果你真的需要知道它是否有小数,你上面的函数并不是很远(尽管语言错误):
function is_decimal( $val )
{
return is_numeric( $val ) && floor( $val ) != $val;
}
回答by Night Owl
If all you need to know is whether a decimal point exists in a variable then this will get the job done...
如果您只需要知道变量中是否存在小数点,那么这将完成工作......
function containsDecimal( $value ) {
if ( strpos( $value, "." ) !== false ) {
return true;
}
return false;
}
This isn't a very elegant solution but it works with strings and floats.
这不是一个非常优雅的解决方案,但它适用于字符串和浮点数。
Make sure to use !== and not != in the strpos test or you will get incorrect results.
确保在 strpos 测试中使用 !== 而不是 != ,否则您将得到不正确的结果。
回答by 77120
if you want "10.00" to return true check Night Owl's answer
如果你想“10.00”返回真实检查夜猫子的答案
If you want to know if the decimals has a value you can use this answer.
如果您想知道小数是否有值,您可以使用此答案。
Works with all kind of types (int, float, string)
适用于所有类型(int、float、string)
if(fmod($val, 1) !== 0.00){
// your code if its decimals has a value
} else {
// your code if the decimals are .00, or is an integer
}
Examples:
例子:
(fmod(1.00, 1) !== 0.00) // returns false
(fmod(2, 1) !== 0.00) // returns false
(fmod(3.01, 1) !== 0.00) // returns true
(fmod(4.33333, 1) !== 0.00) // returns true
(fmod(5.00000, 1) !== 0.00) // returns false
(fmod('6.50', 1) !== 0.00) // returns true
Explanation:
解释:
fmod
returns the floating point remainder (modulo) of the division of the arguments, (hence the (!== 0.00))
fmod
返回参数除法的浮点余数(模),(因此 (!== 0.00))
Modulus operator- why not use the modulus operator? E.g. ($val % 1 != 0)
模运算符- 为什么不使用模运算符?例如($val % 1 != 0)
From the PHP docs:
来自PHP 文档:
Operands of modulus are converted to integers (by stripping the decimal part) before processing.
在处理之前,模数的操作数被转换为整数(通过去除小数部分)。
Which will effectively destroys the op purpose, in other languages like javascript you can use the modulus operator
这将有效地破坏操作目的,在其他语言(如 javascript)中,您可以使用模数运算符
回答by k102
another way to solve this: preg_match('/^\d+\.\d+$/',$number);
:)
解决这个问题的另一种方法preg_match('/^\d+\.\d+$/',$number);
:)
回答by Felix Kling
The function you posted is just not PHP.
您发布的函数不是 PHP。
Have a look at is_float
[docs].
Edit:I missed the "user entered value" part. In this case you can actually use a regular expression:
编辑:我错过了“用户输入的值”部分。在这种情况下,您实际上可以使用正则表达式:
^\d+\.\d+$
回答by Jeff V
I was passed a string, and wanted to know if it was a decimal or not. I ended up with this:
我被传递了一个字符串,想知道它是否是小数。我最终得到了这个:
function isDecimal($value)
{
return ((float) $value !== floor($value));
}
I ran a bunch of test including decimals and non-decimals on both sides of zero, and it seemed to work.
我运行了一堆测试,包括零两边的小数和非小数,它似乎有效。
回答by rybo111
is_numeric
returns true
for decimals and integers. So if your user lazily enters 1
instead of 1.00
it will still return true
:
is_numeric
返回true
小数和整数。因此,如果您的用户懒惰地输入1
而不是1.00
它仍然会返回true
:
echo is_numeric(1); // true
echo is_numeric(1.00); // true
You may wish to convert the integer to a decimal with PHP, or let your database do it for you.
您可能希望使用 PHP 将整数转换为小数,或者让您的数据库为您完成。
回答by Eric_ph
i use this:
我用这个:
function is_decimal ($price){
$value= trim($price); // trim space keys
$value= is_numeric($value); // validate numeric and numeric string, e.g., 12.00, 1e00, 123; but not -123
$value= preg_match('/^\d$/', $value); // only allow any digit e.g., 0,1,2,3,4,5,6,7,8,9. This will eliminate the numeric string, e.g., 1e00
$value= round($value, 2); // to a specified number of decimal places.e.g., 1.12345=> 1.12
return $value;
}
回答by Chaoix
This is a more tolerate way to handle this with user input. This regex will match both "100" or "100.1" but doesn't allow for negative numbers.
这是通过用户输入处理此问题的更宽容的方式。此正则表达式将匹配“100”或“100.1”,但不允许负数。
/^(\d+)(\.\d+)?$/
回答by Van Bien
// if numeric
if (is_numeric($field)) {
$whole = floor($field);
$fraction = $field - $whole;
// if decimal
if ($fraction > 0)
// do sth
else
// if integer
// do sth
}
else
// if non-numeric
// do sth