-2

I want to add following numbers

$res = 0.000000002 + 0.000000002 + 0.000000002;

I got result like this

4.2E-8

can any one explain , how to get 0.000000006

thanks

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
dewshare
  • 45
  • 8

5 Answers5

2

You should get the following result:

echo 0.000000002 + 0.000000002 + 0.000000002;

6.0E-9

Which means you haven't told us the truth.

One way to get 4.2E-8 is to add 0.00000002 + 0.00000002 + 0.000000002; (I removed a zero from two of them).

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
2
//Try using 
echo number_format($res,10); 
Swapnil
  • 592
  • 3
  • 13
2

Try

$res = 0.000000002 + 0.000000002 + 0.000000002;
printf("%0.9f",$res);

or

print(number_format($res,9));

Output

0.000000006
Baba
  • 94,024
  • 28
  • 166
  • 217
0

Please try this .

$res = 0.000000002 + 0.000000002 + 0.000000002;

    echo number_format($res, 9, '.', '');die;

here 9 no is describe after dot how many digit you want to show.

Hardik Patel
  • 706
  • 5
  • 14
  • your code is correct ! but value 9 does not remain the same all time, is any way to replace number 9 to a variable – dewshare Jun 22 '13 at 09:48
-1
$res = 0.0000000002 + 0.0000000002 + 0.0000000002;
    echo exp2dec($res);

    function exp2dec($number) {
            preg_match('/(.*)E-(.*)/', str_replace(".", "", $number), $matches);
            $num = "0.";
            while ($matches[2] > 0) {
                    $num .= "0";
                    $matches[2]--;
            }
            return $num . $matches[1];
    }

    die;

Please try this.

rene
  • 41,474
  • 78
  • 114
  • 152
Hardik Patel
  • 706
  • 5
  • 14