Tjk Posted July 31, 2006 Share Posted July 31, 2006 Is there a way of counting the number of numbers after the decimal point of a sum?For example...[code=php:0]<?$sum= 100/$a_number;//if $sum has so many numbers after a decimal point then do something else do something different?>[/code]Help appreciated as always. :) Quote Link to comment https://forums.phpfreaks.com/topic/16087-count-numbers-after-decimal-point/ Share on other sites More sharing options...
BillyBoB Posted July 31, 2006 Share Posted July 31, 2006 are you trying to make the numbers behind the decimal go away like if the number was 1.1111111111 and u want 1.11 thats called the float might look that up i havnt touched that in php before if thats not wat u need please inform me more :) Quote Link to comment https://forums.phpfreaks.com/topic/16087-count-numbers-after-decimal-point/#findComment-66277 Share on other sites More sharing options...
Tjk Posted July 31, 2006 Author Share Posted July 31, 2006 :) No it isn't.Basically if the $sum has the value 2.333333444445555 then the script will do something like add so much to another variable. But if the value is only 2 then it will do something like takeaway so much to this other variable.So far I've been unable to find a way of doing it.Thanks for any help you can give me. Quote Link to comment https://forums.phpfreaks.com/topic/16087-count-numbers-after-decimal-point/#findComment-66444 Share on other sites More sharing options...
kenrbnsn Posted July 31, 2006 Share Posted July 31, 2006 You can use the explode() function to get that. Here's a short example:[code]<?php$x = 3574/297;echo $x.'<br>';$y = explode('.',$x);echo '<pre>' . print_r($y,true) . '</pre>';echo 'The number of digits after the decimal point is: ' . strlen($y[1]);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/16087-count-numbers-after-decimal-point/#findComment-66456 Share on other sites More sharing options...
AndyB Posted July 31, 2006 Share Posted July 31, 2006 How would you distinguish between 2, 2.0, and 2.0000000000000000000, all of which have different numbers after the 2.Perhaps you want to distinguish between a number that is an integer and one that isn't.The ".. has so many numbers after a decimal point then do something else do something different" is just too vague.Maybe Ken's solution is what you want. If not, explain clearly. Quote Link to comment https://forums.phpfreaks.com/topic/16087-count-numbers-after-decimal-point/#findComment-66461 Share on other sites More sharing options...
Vinze Posted July 31, 2006 Share Posted July 31, 2006 Ken's solution could work, so could this:[code]<?php// Set $number to the number you want to check$length = strlen(stristr(strval($number), '.'));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16087-count-numbers-after-decimal-point/#findComment-66468 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.