brown2005 Posted December 20, 2006 Share Posted December 20, 2006 hi, i have two numbers;$n1 = "453.23423";$n2 = "45323423";i want to echo$n1 has 1 . in it$n2 has 0 . in itany help please... Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/ Share on other sites More sharing options...
chronister Posted December 20, 2006 Share Posted December 20, 2006 [url=http://us2.php.net/manual/en/function.is-float.php]http://us2.php.net/manual/en/function.is-float.php[/url]look at the first comment Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145036 Share on other sites More sharing options...
brown2005 Posted December 20, 2006 Author Share Posted December 20, 2006 i dont think that is really what i needed, as i could have a number that has say;435.345.34thanksrichard Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145038 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 One of things I like about PHP (compared to C for an example), is the fact you cant treat a number both as an integer and as a string :) Use [url=http://www.php.net/strpos]strpos()[/url][code]<?phpif(strpos($number, ".") !== FALSE) echo "Has a dot";else echo "Doesnt have";?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145039 Share on other sites More sharing options...
HuggieBear Posted December 20, 2006 Share Posted December 20, 2006 A regular expression and count would get you the result you're after...[code]<?php$string = "123.456.789";$pattern = "/(\.)/";if (preg_match_all($pattern, $string, $matches)){ echo $string . " has " .count($matches[1]). " . in it";}else { echo $string . " has 0 . in it";}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145043 Share on other sites More sharing options...
brown2005 Posted December 20, 2006 Author Share Posted December 20, 2006 Hi, yeah both of them codes are very useful, thank you very much.What I need to do now if it is possible, is say i have a field in a tablenumberi want to count the number of records in the table where a . is in it?can this be done...? Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145050 Share on other sites More sharing options...
HuggieBear Posted December 20, 2006 Share Posted December 20, 2006 Do you mean in a database?If so, then this should work...[code]SELECT count(column_name) FROM table_name WHERE column_name LIKE '%.%';[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145060 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 Back to your original question, I think a simpler way to code this would be:[code]<?php$string = "123.456.789";$str_exp = explode('.',$string);$num = count($str_exp) - 1;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145064 Share on other sites More sharing options...
brown2005 Posted December 20, 2006 Author Share Posted December 20, 2006 Hi, Huggie, With your code, what if you want to discount duplicate entries...? Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145107 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 Just add "distinct":[code]SELECT DISTINCT count(column_name) FROM table_name WHERE column_name LIKE '%.%'[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31340-how-to-tell-if-there-is-a-in-a-number/#findComment-145112 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.