Adaaam Posted August 30, 2017 Share Posted August 30, 2017 Hi all, I have an array of elements which is dynamically set but for testing purposes I have this code: <?php $items = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32); $count = floor(count($items)); var_dump($count); This returns float(32) but I want to know why it doesn't return the rounded down value of 30? I must be missing something or misunderstanding the function but if someone could explain why this is happening and how I can fix it then that would be great Thanks, Adam Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2017 Share Posted August 30, 2017 Have you tried the manual? count floor Quote Link to comment Share on other sites More sharing options...
Adaaam Posted August 30, 2017 Author Share Posted August 30, 2017 Yes I've tried the manual, maybe you can explain? Quote Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 I'm confused. Why should floor(32) be 30? Quote Link to comment Share on other sites More sharing options...
Adaaam Posted August 30, 2017 Author Share Posted August 30, 2017 Doesn't the function round down? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2017 Share Posted August 30, 2017 What, in this description of the floor() function, Returns the next lowest integer value (as float) by rounding down value if necessary. leads you to think floor(32) will return 30? Quote Link to comment Share on other sites More sharing options...
Adaaam Posted August 30, 2017 Author Share Posted August 30, 2017 Okay fine can you tell me if there is a different function I can use to round 32 to 30 or 49 to 40 or 56 to 50 or 114 to 110? I'm asking for help here... Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2017 Share Posted August 30, 2017 (edited) try echo round(32, -1); //-->30edit: However round(39) = 40, so you will need floor(N/10) * 10 Edited August 30, 2017 by Barand Quote Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 That will round, so 35->40. echo round(32 - 5, -1);Maps 30-39 to 25-34. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2017 Share Posted August 30, 2017 See my edited version Quote Link to comment Share on other sites More sharing options...
Adaaam Posted August 30, 2017 Author Share Posted August 30, 2017 Alright sorted, thanks. Quote Link to comment Share on other sites More sharing options...
kicken Posted August 30, 2017 Share Posted August 30, 2017 Also: $n - ($n % 10)Take your pick, several ways to do it. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2017 Share Posted August 30, 2017 Hmm. What other ways could there be... substr($n, 0, -1) . "0"...is all I've got. I'll keep thinking. Quote Link to comment 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.