检查数字是否为小数
如果用户输入了十进制数(美国方式,带小数点:X.XXX),我需要检查 PHP
是否有可靠的方法来执行此操作?
I need to check in PHP if user entered a decimal number (US way, with decimal point: X.XXX)
Any reliable way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
您可以从 is_float 获得大部分您想要的内容,但是如果您 < em>真的需要知道它是否有小数,上面的函数并不是很远(尽管是错误的语言):
You can get most of what you want from is_float, but if you really need to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):
如果您希望“10.00”返回 true,请检查 Night Owl 的答案
如果您想知道小数是否有值,您可以使用此答案。
适用于所有类型(int、float、string)
示例:
说明:
fmod
返回参数除法的浮点余数(模),(因此是 (!== 0.00))模运算符 - 为什么不使用模数 操作员?例如
($val % 1 != 0)
来自 PHP 文档:
这将有效地破坏运算目的,在其他语言(如 javascript)中,您可以使用模运算符
if you want "10.00" to return true check Night Owl's answer
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)
Examples:
Explanation:
fmod
returns the floating point remainder (modulo) of the division of the arguments, (hence the (!== 0.00))Modulus operator - why not use the modulus operator? E.g.
($val % 1 != 0)
From the PHP docs:
Which will effectively destroys the op purpose, in other languages like javascript you can use the modulus operator
如果您需要知道的是变量中是否存在小数点,那么这将完成工作...
这不是一个非常优雅的解决方案,但它适用于字符串和浮点数。
确保在 strpos 测试中使用 !== 而不是 != ,否则您将得到不正确的结果。
If all you need to know is whether a decimal point exists in a variable then this will get the job done...
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.
解决此问题的另一种方法:
preg_match('/^\d+\.\d+$/',$number);
:)another way to solve this:
preg_match('/^\d+\.\d+$/',$number);
:)您发布的功能不是 PHP。
看看
is_float
[docs]< /sup>。编辑:我错过了“用户输入的值”部分。在这种情况下,您实际上可以使用正则表达式:
The function you posted is just not 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:
我收到一个字符串,想知道它是否是小数。我最终得到了这个:
我运行了一系列测试,包括零两边的小数和非小数,它似乎有效。
I was passed a string, and wanted to know if it was a decimal or not. I ended up with this:
I ran a bunch of test including decimals and non-decimals on both sides of zero, and it seemed to work.
is_numeric
对于小数和整数返回true
。因此,如果您的用户懒惰地输入1
而不是1.00
,它仍然会返回true
:您可能希望使用 PHP 将整数转换为小数,或者让您的数据库为您做这件事。
is_numeric
returnstrue
for decimals and integers. So if your user lazily enters1
instead of1.00
it will still returntrue
:You may wish to convert the integer to a decimal with PHP, or let your database do it for you.
我无法发表评论,但我有这种有趣的行为。
(在 php 在线测试网站上的 v. 7.3.19 上测试)
如果将 50 乘以 1.1 fmod 会得到与预期不同的结果。
如果你做到 1.2 或 1.3 就可以了,如果你做到了其他数字(如 60 或 40)也可以。
I can't comment, but I have this interesting behaviour.
(tested on v. 7.3.19 on a website for php testing online)
If you multiply 50 by 1.1 fmod gives different results than expected.
If you do by 1.2 or 1.3 it's fine, if you do another number (like 60 or 40) is also fine.
这是通过用户输入处理此问题的更容忍的方式。此正则表达式将匹配“100”或“100.1”,但不允许负数。
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.
我用这个:
i use this:
完全是一团糟..但是嘿它有效!
A total cludge.. but hey it works !
找到发布值的简单方法是整数和浮点数,因此
如果您给出 10 或 10.5 或 10.0,这将对您有所帮助,如果您定义任何字符或不带点的特殊字符,结果将成功
the easy way to find either posted value is integer and float so this will help you
if you give 10 or 10.5 or 10.0 the result will be success if you define any character or specail character without dot it will give unsuccess
(int)$value != $value
怎么样?如果
true
则为十进制,如果false
则不是。How about
(int)$value != $value
?If
true
it's decimal, iffalse
it's not.最简单的解决方案是
Simplest solution is
如果您正在使用表单验证。然后在这种情况下形成发送字符串。
我使用以下代码来检查表单输入是否为十进制数。
我希望这对你也有用。
If you are working with form validation. Then in this case form send string.
I used following code to check either form input is a decimal number or not.
I hope this will work for you too.
上述函数的已知问题:
1) 不支持“科学记数法”(1.23E-123)、财政(前导美元或其他)或“尾随 f”(C++ 样式浮点数)或“尾随货币”(美元、英镑等) )
2) 与小数匹配的字符串文件名误报:请注意,例如“10.0”作为文件名无法与小数区分开来,因此如果您尝试从仅字符串,并且文件名与十进制名称匹配且不包含路径,则无法辨别。
Known Issues with the above function:
1) Does not support "scientific notation" (1.23E-123), fiscal (leading $ or other) or "Trailing f" (C++ style floats) or "trailing currency" (USD, GBP etc)
2) False positive on string filenames that match a decimal: Please note that for example "10.0" as a filename cannot be distinguished from the decimal, so if you are attempting to detect a type from a string alone, and a filename matches a decimal name and has no path included, it will be impossible to discern.
也许也尝试看看这个
!is_int()
Maybe try looking into this as well
!is_int()