Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to Convert string to float type in Golang?
Next article icon

How to convert String to Float in PHP ?

Last Updated : 18 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Table of Content

  • Using floatval() function.
  • Using Typecasting
  • Using number_format() function
  • Using sscanf() Function

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

Using number_format() function

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; ?> 

Output
Converted float = 1000.314

Next Article
How to Convert string to float type in Golang?
author
aksrathod07
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-function
  • PHP-Questions

Similar Reads

  • Convert hex string to float in Python
    Converting a hex string to a float in Python involves a few steps since Python does not have a direct method to convert a hexadecimal string representing a float directly to a float. Typically, a hexadecimal string is first converted to its binary representation, and then this binary representation
    3 min read
  • How to convert string to boolean in PHP?
    Given a string and the task is to convert given string to its boolean. Use filter_var() function to convert string to boolean value. Examples: Input : $boolStrVar1 = filter_var('true', FILTER_VALIDATE_BOOLEAN); Output : true Input : $boolStrVar5 = filter_var('false', FILTER_VALIDATE_BOOLEAN); Output
    2 min read
  • How to convert Float to Int in Python?
    In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded. For example: Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.
    5 min read
  • How to Convert string to float type in Golang?
    ParseFloat function is an in-build function in the strconv library which converts the string type into a floating-point number with the precision specified by bit size. Example: In this example the same string -2.514 is being converted into float data type and then their sum is being printed. Once i
    1 min read
  • 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 approaches Table of Content
    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
  • How to Convert String to Number
    Given a string representation of a numerical value, convert it into an actual numerical value. In this article, we will provide a detailed overview about different ways to convert string to number in different languages. Table of Content Convert String to Number in CConvert String to Number in C++Co
    5 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences