I have two strings. the one has a thousand separator as a comma, the other one has a decimal point as a dot.
below is my example code for explanation.
$number1 = "250,000";
$number2 = "1.000";
what I want to do is to multiply those two strings. I somewhat found that PHP thinks the comma as a floating point!
echo number_format($number1 , 3, ".", ",");
//result : 250.000 value type is still a string
I have already used the floatval() method but maybe i havn't used it in a proper way.
please can anyone help me with this problem? I want to multiply those two...
CodePudding user response:
you can do this way
$number1 = "250,000";
$number2 = "1.000";
$number1 = (float) str_replace(',', '', $number1);
$number2 = (float) str_replace(',', '', $number2);
$total = $number1 * $number2;
echo $total; // output => 250000.0
CodePudding user response:
You can use string manipulation like str_replace().
<?php
function parseFloatFromString($string)
{
$string = str_replace(',', '', $string);
$float = floatval($string);
return $float;
}
echo parseFloatFromString("250,000") * parseFloatFromString("1.000");