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.

5
  • I hope it doesn't give you 2.0E-5 as a result, but rather 6.0E-7 Commented Oct 30, 2013 at 9:19
  • Ignoring the spurious cast to float ($ctr already is a float); look at using sprint() if you want to display all the digits, otherwise live with the scientific notation that numbers default to using if there are lots of digits Commented Oct 30, 2013 at 9:20
  • Why is this question tagged mysql and mathematica? Commented Oct 30, 2013 at 9:21
  • 1
    Duplicate of How to add two large fractions in PHP Commented Oct 30, 2013 at 9:57
  • 2
    printf("%0.7f",$ctr); Commented Oct 30, 2013 at 9:59

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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)
@MarkBaker now I can see that aspect of this question :)
@user2341915 printf("%0.7f",$ctr); from @MarkBaker must be what you are looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.