Horje
How to convert String to Float in PHP ?

Strings in PHP can be converted to float very easily. In most use cases, it won’t be required since PHP does implicit type conversion. There are many methods to convert a string into a number in PHP, some of them are discussed below:


Using floatval() function.

Note: The floatval() function can be used to convert the string into float values .

Syntax:

$floatvar = floatval($stringvar)

Return Value: This function returns a float. This float is generated by typecasting the value of the variable passed to it as a parameter.

Example:

PHP
<?php

  // Number in string format
  $stringvar = "1000.314";

  // converts string into float
  $floatvar =  floatval($stringvar);

  // prints the value of above variable as a float
  echo "Converted float = ".$floatvar;

?>

Output:

Converted float = 1000.314

Using Typecasting

Note:  Typecasting is the explicit conversion of data type because the user explicitly defines the data type in which he wants to cast. We convert String into Float.

Syntax:

$floatvar = (float)$stringvar

Example:

PHP
<?php

// Number in string format
$stringvar = "1000.314";

// converts string into float
$floatvar =  (float)$stringvar;

// prints the value of above variable as a float
echo "Converted float = ".$floatvar;

?>

Output:

Converted float = 1000.314

Using number_format() function

Note: The number_format() function is an inbuilt function in PHP that is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure.

Syntax:

string number_format( $number, $decimals, $decimalpoint, $sep )

Return Value: It returns a formatted number in case of success, otherwise it gives E_WARNING if it fails.

Example:

PHP
<?php

// Number in string format
$stringvar = "1000.3145635";

// converts  string into float with 6 decimals
$floatvar =  number_format($stringvar, 6);

// prints the value of above variable as a string
echo "Converted float = ".$floatvar;

?>

Output:

Converted float = 1000.314564

Using sscanf() Function

Another approach to convert a string into a float is by using the sscanf() function. This function parses input from a string according to a specified format.

Syntax:

sscanf($string, $format, &$variable);

Example:

PHP
<?php
$stringvar = "1000.314";
sscanf($stringvar, "%f", $floatvar);
echo "Converted float = " . $floatvar;
?>

Output
Converted float = 1000.314





Reffered: https://www.geeksforgeeks.org


PHP

Related
How to replace String in PHP ? How to replace String in PHP ?
How to convert an Integer Into a String in PHP ? How to convert an Integer Into a String in PHP ?
How to check if a String Contains a Substring in PHP ? How to check if a String Contains a Substring in PHP ?
AsgardCMS - Introduction of in-built and ready to use modules AsgardCMS - Introduction of in-built and ready to use modules
PHP SplPriorityQueue extract() Function PHP SplPriorityQueue extract() Function

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8