Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.