Asked  1 Year ago    Answers:  5   Viewed   13 times

I am attempting to use PHP's printf function to print out a user's storage capacity. The full formula looks something like this:

echo printf("%.02f", ($size/(1024*1024))) . " GB";

Given that $size == (10 * 1024 * 1024), this should print out

10.00 GB

But it doesn't. It prints 10.04 GB. Furthermore,

echo printf("%.02f", 10)

results in


What?! In giving it an integer to convert to a float, it converts 10 to 10.00000009.

How can this be remedied? Obviously, one solution would be to print it out as an integer, but the value will not always be an integer; it may be 5.57 GB, in which case the accuracy of this script is very important.

And umm...

echo printf("%d", 10)

results in


Something is very wrong here.



So apparently printf is not meant to be echoed. At all.

Simply changing the instances of printf to sprintf fixed that problem.

Furthermore, removing the echo, and just running the command as printf("%.02f", 10) does, in fact, print 10.00, however, it should be noted that you cannot append strings to printf like you can with echoing.

If you ask me, PHP should've thrown a syntax error, unexpected T_FUNCTION or something, but I digress.

Thursday, April 1, 2021

Isn't it just because you're echoing a printf?


$array = array("Mo" => "09:30-19:00",  
          "Di" => "09:30-19:00", 
          "So" => "geschlossen");

foreach( $array as $key => $value ){
     printf("%3s:%15s", $key, $value);

 Mo:    09:30-19:00 Di:    09:30-19:00 So:    geschlossen
Thursday, April 1, 2021

I got it.

I wrote:

echo printf(...);

instead of just:


After string produced by printf() i got that string length, which is value returned by printf (in fact - added number was 9 in case of 123.45 value, in 20.4 case it was number 8).

Friday, May 28, 2021

Unless I'm dealing with fractional cents or trillions of dollars (and I'm dealing with neither), should I be concerned at all about displaying and storing (but never doing math on) currency that's been cast to a float? Will I ever come close to the area of having floating point inaccuracies change my figures?

For pure rounding/display purposes, you're safe as long as the absolute floating-point representation error is less than $0.005 (so that rounding to the nearest cent is correct).

With IEEE 754 single-precision, you're safe up to $131,072.00. ($131,072.01 is represented as 131072.015625, which incorrectly rounds up.)

Double precision (which PHP's float uses) doesn't fail until $70,368,744,177,664.01 (which also has .015625 for the cents). You have nothing to worry about.

If the answer to #1 is that I should indeed be concerned, then why is money_format() built this way?

What type should it take? PHP doesn't have a built-in decimal type. Nor do many other languages.

Wednesday, July 28, 2021

DECIMAL(10,2) means that 10 is the maximum number of digits to be used. The 2 specifies that there are two on the right side of the decimal. So this means there are 8 digits left on the left side. See the manual

As for the datatype, you should use double as this is for decimal numbers.

Integer has no decimal chars, so it will floor all numbers. String is for text and not numeric values. Blob is for a binary large object, EG: an image

Sunday, August 22, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :