-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

6
  • Try using printf() rather than echo Commented Jun 22, 2013 at 9:18
  • See: stackoverflow.com/a/13320227/258674 Commented Jun 22, 2013 at 9:20
  • If you want 0.000000006 as result, you should get 6.0E-9. 4.2E-8 means 0.000000042 Commented Jun 22, 2013 at 9:21
  • I used printf( 0.000000002 + 0.000000002 + 0.000000002); result is same, i want to show exact result like 0.000000006 Commented Jun 22, 2013 at 9:24
  • 1
    You shouldn't have got the same result if you used printf( 0.000000002 + 0.000000002 + 0.000000002);; if anything, you should have got a warning.... printf() requires a format mask as the first argument Commented Jun 22, 2013 at 9:35

5 Answers 5

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).

Sign up to request clarification or add additional context in comments.

1 Comment

+ Not the solution but valid point
2
//Try using echo number_format($res,10); 

Comments

2

Try

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

or

print(number_format($res,9)); 

Output

0.000000006 

Comments

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.

1 Comment

your code is correct ! but value 9 does not remain the same all time, is any way to replace number 9 to a variable
-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.