0

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
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16

2 Answers2

0

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)

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
0

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

Jsowa
  • 9,104
  • 5
  • 56
  • 60
  • Thanks, I've looked at the other thread but I'm a bit lost, I'm very new to PHP. I've looked around but I can't find a tutorial about installing a language. Just using that bit of code that you posted isn't enough as you said – Riccardo Morini Jun 08 '20 at 10:24
  • Ok I just realized that for command lines they meant the actual terminal of the computer and not the console window of my IDE, whoops. I'm now installing PHP to my terminal and then I'll install the en-us package, thanks – Riccardo Morini Jun 08 '20 at 10:38
  • Make sure if you really need to change your locale and not just include validation for your form to allow only acceptable numbers. How to configure system locales: https://www.tecmint.com/set-system-locales-in-linux/ If it will resolve your problem just close the question. – Jsowa Jun 08 '20 at 10:42