PHP String to Float
$rootbeer = (float) $InvoicedUnits;
Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.
PHP: convert string to float (only if string represents a float)
You can use is_numeric() function to check variable which might contain a number.
For example:
$a = "1.23";
if (is_numeric($a)) {
$a = (float)$a;
}
$b = "1.2 to 1.3";
if (is_numeric($b)) {
$b = (float)$b;
}
var_dump([
'a' => $a,
'b' => $b
]);
Output
array (size=2) 'a' => float 1.23 'b' => string '1.2 to 1.3'
(length=10)
When I change the string into float I always get 0
Your problem is the content of $string
.
In PHP, every time you convert a string to a float, be it by casting (float) $string
, or floatval($string)
, you will always get 0
if the first character of the string is not numeric.
From PHP docs: String conversion to numbers
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero)
Double-check the content of $string
. Probably you have some spurious characters at its beginning.
PHP convert String number with whitespace to Float
Most likely you have a non visible character as the thousand separator character. It's more safe to filter all characters except digits and .
when sanitizing the input string. This can be achieved with the preg_replace
function.
Your script can be modified to something like this:
$a = "3 999.99";
$b = "1 500.11";
$aa = floatval(preg_replace('/[^0-9.]/', '', $a));
$bb = floatval(preg_replace('/[^0-9.]/', '', $b));
$c = $aa - $bb;
echo $aa . ' - ' . $bb . ' = ' . $c;
Additionally, it's a bit dangerous and non exact to use floats when handling money in particular. Please have a look at this repository which helps a lot with this problem: https://github.com/moneyphp/money
PHP String to Float
$rootbeer = (float) $InvoicedUnits;
Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.
PHP Convert String into Float/Double
Surprisingly there is no accepted answer. The issue only exists in 32-bit PHP.
From the documentation,
If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
In other words, the $string
is first interpreted as INT, which cause overflow (The $string
value 2968789218 exceeds the maximum value (PHP_INT_MAX
) of 32-bit PHP, which is 2147483647.), then evaluated to float by (float)
or floatval()
.
Thus, the solution is:
$string = "2968789218";
echo 'Original: ' . floatval($string) . PHP_EOL;
$string.= ".0";
$float = floatval($string);
echo 'Corrected: ' . $float . PHP_EOL;
which outputs:
Original: 2.00
Corrected: 2968789218
To check whether your PHP is 32-bit or 64-bit, you can:
echo PHP_INT_MAX;
If your PHP is 64-bit, it will print out 9223372036854775807
, otherwise it will print out 2147483647
.
Related Topics
How to Get Int Instead String from Form
Is There a Good PHP Geolocation Service
Laravel: Using Try...Catch with Db::Transaction()
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
What Is the Best Practice to Export Canvas with High Quality Images
Will Enabling Xdebug on a Production Server Make PHP Slower
Str_Replace() on Multibyte Strings Dangerous
Unset a Column in a Multidimensional Array
How to Redirect After Download in Laravel
Sorting Multidim Array: Prioritize If Column Contains Substring, Then Order by a Second Column
What Does Double Question Mark () Operator Mean in PHP
Disabling Strict Standards in PHP 5.4
How to Identify Server Ip Address in PHP
Get Most Recent Date from an Array of Dates in "Y-M-D H:I:S" Format
Why Is Constructing Pdo Connection Slow
Can PHP and ASP.NET Run Together Within the Same Web Site in Iis 7.5