gish Posted February 27, 2008 Share Posted February 27, 2008 hi everyone, I have a script that will sometimes post a number that has a decimal point. But the script below only wants whole numbers. In the code below how do i get to $testwholenumber if to check to see it is a decimal point and test it properly. if ($testwholenumber <= $testwholenumber.1){ echo "Sorry all numbers have be whole for example 12.3 will not be accepted"; }elseif{ #myscript } gish Quote Link to comment https://forums.phpfreaks.com/topic/93270-testing-to-see-if-a-number-has-a-decimal-point/ Share on other sites More sharing options...
cyrixware Posted February 27, 2008 Share Posted February 27, 2008 substr Quote Link to comment https://forums.phpfreaks.com/topic/93270-testing-to-see-if-a-number-has-a-decimal-point/#findComment-477735 Share on other sites More sharing options...
markjoe Posted February 27, 2008 Share Posted February 27, 2008 Would it be acceptable to round the number and just take the integer part of it? floor(12.3) gets converted to 12 Or you can test it as a string with strpos(). Quote Link to comment https://forums.phpfreaks.com/topic/93270-testing-to-see-if-a-number-has-a-decimal-point/#findComment-477848 Share on other sites More sharing options...
gish Posted February 27, 2008 Author Share Posted February 27, 2008 thankyou for your help everyone I chose strpos(). It is so simple all i have to do is tell the function to find "." . Quote Link to comment https://forums.phpfreaks.com/topic/93270-testing-to-see-if-a-number-has-a-decimal-point/#findComment-478440 Share on other sites More sharing options...
gish Posted February 27, 2008 Author Share Posted February 27, 2008 I having trouble with this piece of code can someone tell me please. If a decimal point is found in $testwholenumber it will make the $falseortrue == true and the if statement won't work. Is this right $testwholenumber = $NumOne / $NumTwo; echo $testwholenumber; $findme = '.'; $falseortrue = strpos($testwholenumber, $findme); if ($falseortrue == false){ #mycode } gish Quote Link to comment https://forums.phpfreaks.com/topic/93270-testing-to-see-if-a-number-has-a-decimal-point/#findComment-478492 Share on other sites More sharing options...
gish Posted February 27, 2008 Author Share Posted February 27, 2008 sorry I found the error $numtwo was a always 0 which always caused a whole number. the above script works fine Quote Link to comment https://forums.phpfreaks.com/topic/93270-testing-to-see-if-a-number-has-a-decimal-point/#findComment-478500 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.