Jump to content

seperate the float and integer value of a result.


art15

Recommended Posts

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

 

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?

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. :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.