0

Fair notice there is apparently another topic that talks about this: Printf width specifier to maintain precision of floating-point value But the information there is too complicated for me to get. I'm kinda new to this.

The task I have is to print the answer to some calucations. However the answers vary in their precision.

%f - prints only 6 symbols and doesn't give me the exact numbers.

%.10f - or another precision modifier doesn't give me the exact number because for some reason somehow it gives even greater presionion than it should be able to. A result of 12442.2442010256 somehow became 12442.244201025640.

%s - also did not work and I don't quite understand why. It should have printed this 569.668717948718 but it print 569.66871794872. No rounding or spcifying anything here.

Than I tried round() but it got overwritten by format specifiers of printf() so I removed them.

$result = round($result, 16);
printf($result);

This also did not work as most of the results I got were 1 shorter than accurate results due to the rounding I suppect.

Than I had the idea to somehow set it to a high precision like 16, than convert it to string, than trim the zeroes and than print that, but I have no idea how to do that.

And here we are. Advice is greatly appreciated and I'll be very thankful if its in simpler terms than the other topic.

Community
  • 1
  • 1
cybera
  • 351
  • 2
  • 17
  • Give examples of your calculations not just the results. – AbraCadaver Oct 25 '16 at 18:05
  • @AbraCadaver What is the point of that? Here are some examples of the math anyways: $bitcoin = 1; $yuan = 5; $commission = 5; $leva = ($bitcoin * 1168) + (($yuan * 0.15) * 1.76); $euro = $leva / 1.95; $result = $euro - ($euro * $commission / 100); $result = round($result, 16); printf($result); Result I should be getting for this is 569.668717948718 Result I am getting for this is 569.66871794872 How do I format here? – cybera Oct 25 '16 at 18:12
  • 1
    The point is that it's not the "format" http://stackoverflow.com/questions/588004/is-floating-point-math-broken – AbraCadaver Oct 25 '16 at 18:15
  • @AbraCadaver What can I do about it? – cybera Oct 25 '16 at 18:18
  • Probably this, http://php.net/manual/en/book.bc.php, or somebody may have a better answer given your inputs. – AbraCadaver Oct 25 '16 at 18:21
  • Some dude, marked it as a duplicate, does that close it? – cybera Oct 25 '16 at 18:23

0 Answers0