简体   繁体   English

如何在 laravel 中检查给定数字是否为十进制?

[英]How to check given number is decimal or not in laravel?

I want to check weather given number is decimal or not if it's decimal it should return decimal or else it should return integer.我想检查天气给定的数字是否是十进制的,如果它是十进制的,它应该返回十进制,否则它应该返回 integer。

I tried many solutions from the internet it's failing some conditions, please help me to acheive this thing.我从互联网上尝试了许多解决方案,但都未能满足某些条件,请帮助我实现这一目标。

It should return decimal if it's this format also 10.00, 10.0,99.000 .如果它也是这种格式,它应该返回十进制10.00, 10.0,99.000

$value=$request->amount;//99.99,99.00...
if(gettype($value)){
 if(decimal){
 //My logic goes here.
}
if(integer){
//My logic
}
}

Here is what you want:这是你想要的:

$value=$request->amount;

if(fmod($value, 1) !== 0.00){
    //It is decimal, so the decimal logic goes here.
} else {
    // It is an Intereger, so the ineteger logic goes here.
}

fmod(x,y) is a PHP function that returns the remainder of x/y. fmod(x,y) 是返回 x/y 余数的 PHP function。

You simply set y=1, the number (here is x) is an Integer only if the remainder of x/1 equals 0.00.您只需设置 y=1,只有当 x/1 的余数等于 0.00 时,数字(这里是 x)才是 Integer。

For example:例如:

the output of fmod(10.05,1) is 0.05 so it is decimal the output of fmod(5,1) is 0 so it is an integer fmod(10.05,1) 的 output 是 0.05,所以它是十进制 fmod(5,1) 的 output 是 0,所以它是 Z157DB7DF530023575515D366EC

If you want to learn more about fmod function, pay a visit to fmod function description如果您想了解更多关于 fmod function 的信息,请访问fmod function 说明

Note: you can even pass a string to fmod function and it will automatically change to a number.注意:您甚至可以将字符串传递给 fmod function,它会自动更改为数字。

For example:例如:

the output of fmod('34.04',1) is 0.04 and it is decimal fmod('34.04',1) 的 output 为 0.04,为十进制

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, you can use the following function:你可以从is_float得到大部分你想要的,但如果你真的需要知道它是否有小数,你可以使用下面的 function:

function is_decimal( $val )
{
    return is_numeric( $val ) && is_float( $val ) != $val;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM