php convert to float with 2 decimals code example

Example 1: php float 2 decimais

$foo = "105";
echo number_format((float)$foo, 2, '.', '');

Example 2: string to float php

$floatValue = floatval("1.0");

Example 3: number format to float php

$num = '1,200,998.255';

########## FOR FLOAT VALUES ###########################

echo filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);

#output  : 1200998.255

########## FOR INTEGER VALUES ###########################

echo filter_var($num, FILTER_SANITIZE_NUMBER_INT);

#output  : 1200998

Tags:

Php Example