| by Arround The Web | No comments

How to Convert String to Float in PHP

String-to-float conversion is a common task that allows users to transform textual data into a numeric representation. This process is beneficial when dealing with user inputs or data retrieval from external sources, where numbers are typically stored as strings. Converting these strings to a float will help users perform mathematical operations and utilize the numeric value for various calculations.

This article is a detailed guide to converting strings to float in PHP.

Convert String to Float in PHP

You can perform string-to-float conversions in PHP:

1: Using Typecasting in PHP

The typecasting is an explicit conversion type where the users can explicitly convert the data type to which he wants to convert. It is one of the ideal ways to convert a string into a float number without using a function.

If you are using the typecasting method to convert the string to float, use the float followed by the string variable name.

In the following example, we have declared and initialized the stringvar with the value “12.67”. We then used typecasting to convert the string to float. The output of the code is printed on the console via the echo statement:

<?php
$stringvar = "12.67";
$floatvar = (float)$stringvar;
echo "The converted float value is: ".$floatvar;
?>

2: Using floatval() function in PHP

Another easiest way to convert the string to float in PHP is through the built-in method floatval(). It takes the string value as an argument and returns its float value.

Consider the following example of using the floatval() function to convert the string to float in PHP:

<?php
$stringvar = "2.314";
$floatvar = floatval($stringvar);
echo "The converted float value is: ".$floatvar;
?>

3: Using number_format() Function

The number_format() function in PHP is also used to convert the string to a number. This function returns the formatted number of provided string on successful execution and displays the E_WARNING on failure.

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

It accepts four parameters as an argument, the number is the required parameter that needs to be converted, the decimal is the optional parameter that is used to specify the decimals, the decimal point is also an optional parameter that is used to specify the string to use for decimal points and sep specifies the string used for thousand separators, it is also an optional parameter.

Consider the following example of converting the string to a float number up to 4 decimals:

<?php
$string = "156.9857333324";
$floatvar = number_format($string, 4);
echo "The Converted float value is: ".$floatvar;
?>

Final Thoughts

PHP is a server-side scripting language, designed especially for creating interactive websites. PHP provides various built-in functions to perform any specific action. If you want to convert the string to a number, you can do it through the methods like typecasting, floatval function, and number_format(). Understanding these functions will help you perform complex string-to-number conversions in PHP with ease.

Share Button

Source: linuxhint.com

Leave a Reply