1$stringVal = "12.06";
2$stringConvertedToFloat = floatval( $stringVal );
3// The floatval function will return the argument converted
4// to a float value if the value can be converted.
5// IF the value cannot be converted these are the values that will be
6// returned:
7// Empty Array: returns 0. eg: floatval([]);
8// Non-Empty Array: returns 1. eg: floatval(["ab", "12"])
9// String with a non-numeric value as the left most character: returns 0. eg: floatval("ab12")
10// String with one or more numeric values as the left most characters: returns those characters as a float. eg: floatval("12ab1") will return 12.
11// Oh the joys of php
1phpCopy<?php
2$mystring = "0.5674";
3echo("This float number is of string data type ");
4echo($mystring);
5echo("\n");
6$myfloat = floatval($mystring);
7echo("Now, this float number is of float data type ");
8echo($myfloat);
9?>
10
1phpCopy<?php
2$mystring = "0.5674";
3echo("This float number is of string data type ");
4echo($mystring);
5echo("\n");
6$myfloat = number_format($mystring, 4);
7echo("Now, this float number is of float data type ");
8echo($myfloat);
9?>
10
1$num = '1,200,998.255';
2
3########## FOR FLOAT VALUES ###########################
4
5echo filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
6
7#output : 1200998.255
8
9########## FOR INTEGER VALUES ###########################
10
11echo filter_var($num, FILTER_SANITIZE_NUMBER_INT);
12
13#output : 1200998
1phpCopy<?php
2$mystring = "0.5674";
3echo("This float number is of string data type ");
4echo($mystring);
5echo("\n");
6$myfloat = (float) $mystring;
7echo("Now, this float number is of float data type ");
8echo($myfloat);
9?>
10