How to convert String to Float in PHP ?
Last Updated :
18 Sep, 2024
Converting a string to a float in PHP is a common requirement when handling numeric data stored in string format. This conversion allows the numeric string to be used in calculations or comparisons, ensuring accurate manipulation of data within the program.
There are many methods to convert a string into a number in PHP, some of them are discussed below:
Using floatval() function
The floatval() function in PHP converts a string to a floating-point number. It extracts the numeric value from the string, ignoring non-numeric characters. This approach is simple and efficient for converting string representations of numbers into usable float values.
Syntax
$floatvar = floatval($stringvar)
Example: In this example we converts a number in string format ("1000.314") into a float using the floatval() function and then prints the converted float value (1000.314).
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
Typecasting in PHP allows directly converting a string to a float by prefixing the string with (float). This approach forces the string to be interpreted as a floating-point number, enabling mathematical operations or further manipulation of the data.
Syntax
$floatvar = (float)$stringvar
Example: In this example we converts a string ("1000.314") into a float using typecasting (float). It then prints the converted float value. Typecasting directly forces the string to be treated as a floating-point number.
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
The number_format() function in PHP is primarily used to format a number with grouped thousands and specified decimal points. While it formats a string as a number, it returns a string, requiring additional conversion if a float is needed.
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 )
Example: In this example we use the number_format() function to format a string ("1000.3145635") into a float with 6 decimal places, but it returns the value as a string.
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
The sscanf() function in PHP parses input based on a specified format. When converting a string to a float, sscanf() reads the string and extracts the float value, making it an effective way to handle string-to-float conversions.
Syntax:
sscanf($string, $format, &$variable);
Example: In this example we use the sscanf() function to parse a string ("1000.314") into a float. It then prints the converted float value using the format specifier %f.
PHP
<?php
$stringvar = "1000.314";
sscanf($stringvar, "%f", $floatvar);
echo "Converted float = " . $floatvar;
?>
OutputConverted float = 1000.314
Similar Reads
How to convert DateTime to String using PHP ? Converting a `DateTime` to a string in PHP involves formatting the `DateTime` object into a human-readable format using the `format()` method. This process allows you to represent dates and times as strings in various formats, such as Y-m-d H:i:s. There are some following approachesTable of ContentB
4 min read
How to convert string into float in JavaScript? In this article, we will convert a string into a float in Javascript. We can convert a string into a float in JavaScript by using some methods which are described below: Methods to Concert String into Float:Table of Content Method 1: By using Type Conversion of JavaScriptMethod 2: By using parseFloa
6 min read
Convert String with Comma To Float in Python When working with data in Python, it's not uncommon to encounter numeric values formatted with a mix of commas and dots as separators. Converting such strings to float is a common task, and Python offers several simple methods to achieve this. In this article, we will explore five generally used met
3 min read
Cannot Convert String To Float in Python Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con
3 min read
How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are
4 min read
How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some
3 min read