Casting numeric string with leading currency symbol always becomes 0

I have this code and to cast a currency-prefixed number to an integer.

$t = "€2000";
$venc = (int)$t;
echo $venc; // actually echo is 0 and I want 2000 (remove symbol)

The output is 0 and not 2000, so, the code is not working as I expect.

What is the reason for (int)$t; to not echo 2000?


  • casting routine does not remove invalid characters, but start from the beginning and stops when first invalid character is reached then convert it to number, in your case Euro sign is invalid and it is the first character thus resulting number is 0.


    you could try (int)preg_replace('/\D/u','',$t);

    however if you are dealing with currencies you should not forget that they are not integers but floats (though using floats for currencies is not the best idea because of precision issues, i.e. 0.1 + 0.2 !== 0.3)