GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP: How do you convert from EXPONENT_DNUM to LNUM format?? (https://gfy.com/showthread.php?t=458024)

Varius 04-20-2005 11:26 AM

PHP: How do you convert from EXPONENT_DNUM to LNUM format??
 
I'm having a very annoying problem this morning....

If I increment a variable by small floats (like 0.00001 lets say) in a loop, the vairable becomes EXPONENT_DNUM format. I would like to keep it in LNUM format. Any ideas?

I've made a simple loop here to show an example:

PHP Code:

<?PHP

$var 
0;

for (
$i=0;$i<5;$i++) {
    
$var += 0.000004;
}

echo 
$var;

?>

That gives me "2E-05" instead of the 0.00002 I"m looking for.

Varius 04-20-2005 11:31 AM

I do have one way to make it work, but I'd prefer finding out the true method...my way is just a workaround :)

I replace the last line by this instead, and then it works:

PHP Code:

$num split("-",$var);

echo 
number_format($var,$num[1]); 


pstation 04-20-2005 11:34 AM

use the force

azguy 04-20-2005 11:36 AM

Try to add 1 to it

swedguy 04-20-2005 11:39 AM

err......

Varius 04-20-2005 11:41 AM

Quote:

Originally Posted by azguy
Try to add 1 to it

No luck...also tried * 1, using settype to make sure they are equivalent types and few other things.

The weird part is, if I only add a value once, its fine.

ie.
PHP Code:

$var 0;
$var += 0.00004;

echo 
$var

will give me 0.00004 as output.

Once I loop on it though and add more values, it changes format.

Weird, no one on IRC or google could solve it...my fix above though works for my purposes..

azguy 04-20-2005 11:42 AM

Quote:

Originally Posted by Varius
No luck...also tried * 1, using settype to make sure they are equivalent types and few other things.

The weird part is, if I only add a value once, its fine.

ie.
PHP Code:

$var 0;
$var += 0.00004;

echo 
$var

will give me 0.00004 as output.

Once I loop on it though and add more values, it changes format.

Weird, no one on IRC or google could solve it...my fix above though works for my purposes..

I'd stick to what works then :)

jeffs 04-20-2005 11:43 AM

You can use printf("%f", $var) or printf("%.5f", $var) instead of echo $var.

mortenb 04-20-2005 12:04 PM

PHP Code:


<?PHP
bcscale
(6);
$var    0;

for (
$i=0;$i<5;$i++) {
    
$var    bcadd($var,1.000004);
}
$var    bcsub($var,5);

echo 
$var;
?>

dunno if that is close enough for you..


All times are GMT -7. The time now is 06:22 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123