1

I was working with floats in PHP when I noticed that one server I am on treats them differently. How do I gain consistency between the two servers?

EXAMPLE CODE

$var = "6.15";

var_dump(round($var, 2));
var_dump(floatval($var));

ini_set('serialize_precision', 3); //I don't understand this and would rather not use it

var_dump(round($var, 2));
var_dump(floatval($var));

SERVER 1 RESULTS - Linux - PHP Version 8.1.12

float(6.1500000000000004)
float(6.1500000000000004)
float(6.15)
float(6.15)

SERVER 2 RESULTS - Linux - PHP Version 7.3.25

float(6.15)
float(6.15)
float(6.15)
float(6.15)
Paul
  • 11
  • 1
  • I just realized in the PHP info that serialize_precision is set to "17" on SERVER 1 and "-1" on SERVER 2. – Paul Apr 26 '23 at 18:28
  • 1
    `serialize_precision` _is_ the answer, and either that or the more general `precision` are what differs between the servers. The setting simply denotes how many decimal places are rendered. The actual numerical value is the same on both machines, and the very small fractional value is explained here: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Sammitch Apr 26 '23 at 18:29
  • 1
    If you talk of precision, don't use float... – Honk der Hase Apr 26 '23 at 23:22
  • Thanks for the responses. Setting the serialize_precision to -1 on both servers fixed my issue. A library I need to interact with updated recently to require floats as inputs. – Paul Apr 27 '23 at 12:33

0 Answers0