How to gain consistency with floats in PHP between servers? - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, April 27, 2023

How to gain consistency with floats in PHP between servers?

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)


source https://stackoverflow.com/questions/76113903/how-to-gain-consistency-with-floats-in-php-between-servers

No comments:

Post a Comment