0

I have "0.05" as a string in php. How can I turn it into a number with intval()?

Wenn I try this

intval("0.05")

it just gives

0

How can I solve this?

Tom Rowe
  • 13
  • 3
  • 1
    You can't turn a decimal representation into a decimal with a function that only returns integers. You might want `floatval` instead. – Cᴏʀʏ Jan 03 '17 at 18:32
  • `(double)"0.05";` - as Cory said an int can't have decimals... – Gerrit0 Jan 03 '17 at 18:34

2 Answers2

2

Type cast it as a float

$x = "0.05";
$y = (float)$x;
var_dump($y); // float(0.05)

Note that this may require some finessing as floats are notoriously imprecise

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
1

You are using int-val. This will give you an integer answer. Since you are working with a decimal fraction, what you need is floatval.

floatval("0.05");

See the documentation

Machavity
  • 30,841
  • 27
  • 92
  • 100
shalvah
  • 881
  • 10
  • 21