2

i am trying adding numbers like this

$ctr = 0.0000001 + 0.0000001+ 0.0000001+ 0.0000001+ 0.0000001+ 0.0000001; 
echo floatval($ctr);

it gives result like this 2.0E-5

i have tried, number_format, round etc... but result is same, it should be 0.0000006

please help me to make this happened.

agentp
  • 6,849
  • 2
  • 19
  • 37
dewshare
  • 45
  • 8
  • I hope it doesn't give you 2.0E-5 as a result, but rather 6.0E-7 – Mark Baker Oct 30 '13 at 09:19
  • Ignoring the spurious cast to float ($ctr already is a float); look at using [sprint()](http://www.php.net/manual/en/function.sprintf.php) if you want to display all the digits, otherwise live with the [scientific notation](http://en.wikipedia.org/wiki/Scientific_notation) that numbers default to using if there are lots of digits – Mark Baker Oct 30 '13 at 09:20
  • Why is this question tagged [tag:mysql] and [tag:mathematica]? – eggyal Oct 30 '13 at 09:21
  • 1
    Duplicate of [How to add two large fractions in PHP](http://stackoverflow.com/questions/17249130/how-to-add-two-large-fractions-in-php) – Mark Baker Oct 30 '13 at 09:57
  • 2
    `printf("%0.7f",$ctr);` – Mark Baker Oct 30 '13 at 09:59

1 Answers1

0

I think you can try BCMath as it advertises:

For arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings

A sample code:

$sum = bcadd('0.0000001','0.0000001',7); // 7 = num of digits after decimal point
echo $sum; // 0.0000002
Kita
  • 2,604
  • 19
  • 25
  • Surely a bit overkill for the values that OP is working with... the problem seems to be a lack of understanding of scientific notation (e.g. basic mathematics) – Mark Baker Oct 30 '13 at 09:22
  • @MarkBaker now I can see that aspect of this question :) – Kita Oct 30 '13 at 09:36
  • @user2341915 ``printf("%0.7f",$ctr);`` from @MarkBaker must be what you are looking for. – Kita Nov 01 '13 at 22:59