art15 Posted May 21, 2008 Share Posted May 21, 2008 Hi, I have a result from the database which is a float value for eg. 45.767. I have used the intval function to strip the int value which gives me 45 which I can hold in a variable say $a. Now if I use 45.767-45 which gives me 0.767. Is it a way to just get the value 767 in variable $b which will be the float value. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/106690-seperate-the-float-and-integer-value-of-a-result/ Share on other sites More sharing options...
abid786 Posted May 21, 2008 Share Posted May 21, 2008 Dont know if there is a function for it, but you could easily make one. Do 45.767 minus $a to get 0.767. Then a do while loop which multiplies the 0.767 by 1 until the answer mod 10 = 0. (Pseudo code) $decimal = $dbnum (which is 45.767) - $a (which is 45) do while $x % 1 == 0 { $x = 10*$decimal; } By the end, $x would be 767. Would this work? Quote Link to comment https://forums.phpfreaks.com/topic/106690-seperate-the-float-and-integer-value-of-a-result/#findComment-546925 Share on other sites More sharing options...
roopurt18 Posted May 21, 2008 Share Posted May 21, 2008 Without caring about performance: list( $front, $back ) = explode( '.', $decimalValue ); $front = intval( $front ); $back = intval( $back ); Quote Link to comment https://forums.phpfreaks.com/topic/106690-seperate-the-float-and-integer-value-of-a-result/#findComment-546936 Share on other sites More sharing options...
neylitalo Posted May 21, 2008 Share Posted May 21, 2008 abid786: The following are always true. x * 1 = x x % 1 = 0 So your example is actually a fine example of an infinite loop. And while this is a huge hack and completely dependent on PHP's disregard for types, it works: <?php $float = 45.767; $a = intval($float); // I prefer floor, but that's all it is - a preference. $b = ($float - $a) * (pow(10, strlen($int)-2)); // strlen($int)-2 takes care of the "0." that automatically gets prepended. ?> roopurt18: I think mine is the clear choice here. It impresses way more people when you look at a bunch of cryptic nonsense and say "Yeah, I know what that means." Yours is too easy to understand. Quote Link to comment https://forums.phpfreaks.com/topic/106690-seperate-the-float-and-integer-value-of-a-result/#findComment-546941 Share on other sites More sharing options...
art15 Posted May 22, 2008 Author Share Posted May 22, 2008 You guys are great. Thanks to both of you. Hey roopurt18, I liked the one you suggested. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/106690-seperate-the-float-and-integer-value-of-a-result/#findComment-546950 Share on other sites More sharing options...
roopurt18 Posted May 22, 2008 Share Posted May 22, 2008 lol Neal. Too bad this wasn't C; I could have written a cute little solution that just doesn't translate to PHP very well. Quote Link to comment https://forums.phpfreaks.com/topic/106690-seperate-the-float-and-integer-value-of-a-result/#findComment-547006 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.