convert string to float in php code example
Example 1: convert a value to a float in php
$stringVal = "12.06";
$stringConvertedToFloat = floatval( $stringVal );
Example 2: string to float php
$floatValue = floatval("1.0");
Example 3: number format to float php
$num = '1,200,998.255';
echo filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
echo filter_var($num, FILTER_SANITIZE_NUMBER_INT);
Example 4: Convert String to Float in PHP
phpCopy<?php
$mystring = "0.5674";
echo("This float number is of string data type ");
echo($mystring);
echo("\n");
$myfloat = floatval($mystring);
echo("Now, this float number is of float data type ");
echo($myfloat);
?>
Example 5: Convert String to Float in PHP
phpCopy<?php
$mystring = "0.5674";
echo("This float number is of string data type ");
echo($mystring);
echo("\n");
$myfloat = number_format($mystring, 4);
echo("Now, this float number is of float data type ");
echo($myfloat);
?>
Example 6: Convert String to Float in PHP
phpCopy<?php
$mystring = "0.5674";
echo("This float number is of string data type ");
echo($mystring);
echo("\n");
$myfloat = (float) $mystring;
echo("Now, this float number is of float data type ");
echo($myfloat);
?>