简体   繁体   中英

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

You are probably using comma and not point to separate decimals.

"1,23" (which will be casted to 1 ) is different to "1.23" (which will be casted to 1.23 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM