php - Cast double with number_format

Asked

Viewed 331 times

1

When executing:

 $number = 50/3;   
 var_dump(number_format($number, 2, '.', ''));

is returned

string(5) "16.67"

however I need this value to be given in decimal type, so I run the cast to (double).

However, when performing the cast:

$num = (double)number_format($number, 2, '.', '');
var_dump($num);

php returns me:

float(16.666666666667)

Thus, it disregards function formatting

number_format

Remember that this happens on the production server. On the development server, this cast (double) respects number_format.

I’ve replicated the same production data on the development server, but to no avail.

inserir a descrição da imagem aqui

Production version of PHP: PHP 7.2.15-1+0~20190209065041.16+jessie~1.gbp3ad8c0

PHP Version in Development: PHP 7.0.33-0+deb9u1

Any suggestions?

  • If you don’t cast to double after number_format the value gets the right number of decimals in production?

  • Yes. I did the test in Production: I removed the cast for double and it got the correct number of decimals.

  • Face really ta strange this ae, I tested site and works normal, who knows you try to use something else instead of number_format, this here should bring the same result. var_dump(float) sprintf('%0.2f', $number));

  • I get it. I sincerely believe there is some bug in the PHP version I’m using in production. In Development (Debian 9) we don’t face this problem. But in Production (Debian 8 Jessie) is the first time I face something like this. Anything we do will migrate the production server system to a newer version of Debian, or update PHP

  • 1

    I tested in this environment: PHP 7.2.15-1+ubuntu18.04.1+deb.sury.org+1, and it worked normal.

  • But another thing you can still try is to use round($number, 2).

  • I tested round($number, 2) and persists.

  • ok, just one more thing then, before calling number_format put it here, ini_set('Precision', 2);

Show 3 more comments

2 answers

0


After several tests on other Linux distributions and even using XAMPP for Windows, we could see that it is not a programming bug, logic error or something like that, but a version-specific BUG

PHP 7.2.15-1+0~20190209065041.16+jessie~1.gbp3ad8c0

ambient Debian 8 Jessie. We have concluded in our company that it is necessary to migrate our system to a more stable version, such as the Debian 9 "Stretch", for example.

Although these decimal places do not affect the whole use of the system, because in our databases we use the type DECIMAL(9,2), which would limit data entry to more than 2 decimal places and the system has treated FRONT-END, but in BACK-END we would have this "deficiency".

Get the tip and experience for the next users

0

The function number_format() accepted only float, if you want to use double does everything with the double before you cut to the number of decimal places you want.

$number = 50/3;
//Fazer tudo o que queres com $number como double antes de passares para float.  
var_dump(number_format((float)$number, 2, '.', ''));
  • No, number_format accepts string, int, double and float.

  • @fajuchem http://php.net/number_format according to the documentation the first element has to be "float $number"

  • Yes, the documentation asks for a float, but it is a common practice to pass the numbers on other types such as string, I could not find where this information is, but it should cast to float internally. In addition your solution propose to change the double to float that in the end is the same thing, as described http://php.net/manual/en/language.types.float.php.

  • Both are in the order of real numbers. As I said in the main scope of the message, this happens with version PHP 7.2.15-1+0~20190209065041.16+jessie~1.gbp3ad8c0 (Debian 8) - Already in version PHP 7.0.33-0+deb9u1 (Debian 9) everything happens naturally and as accurate

  • But if I simply make number_format((float)$number, 2, '.', '') the final return of number_format is a string and not a (double)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.