Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

floatval($moneyString) returning 0

Code:

var_dump($prodCost);  
$prodCost = (float)$prodCost;  
var_dump($prodCost);

result:

string(5) "$4.57"
float(0)

What am I doing wrong here? I am looking for the result to be float(4.57).

like image 616
Bead Avatar asked Jan 22 '26 01:01

Bead


1 Answers

The $-sign is not valid for numbers, PHP will break off after the first non-numeric char. Try this:

php > echo floatval(ltrim('$4.57', '$'));
4.57
like image 98
TimWolla Avatar answered Jan 26 '26 01:01

TimWolla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!