Skip to content
Advertisement

Casting a value into FLOAT in PHP loses the decimal points. Basically turning the value into an INT

I’m calling a PHP form, the float value is received correctly, but if I cast it into a FLOAT, either immediately of afterwards, the value loses all decimal point numbers, and I don’t want that. I tried using floatval but same result.

//code

$time_in_seconds = $_POST["time_in_seconds"];//form value is 1.23f

echo $time_in_seconds;//echo 1.23

$time_in_seconds = (float)$time_in_seconds;

echo $time_in_seconds;//echo 1

Advertisement

Answer

Install proper locale settings on your machine. Probably you have installed IT (Italy) locale file and there is , as a decimal separator so casting doesn’t “see” that you pass it float number.

setlocale(LC_ALL, 'en-US.utf8');

But it’s not sufficient to type this line into your code. You must also install locale file. More info: PHP setlocale has no effect

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement