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
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Decimal is_finite() method
Next article icon

Float type and its methods in python

Last Updated : 21 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A float (floating-point number) is a data type used to represent real numbers with a fractional component. It is commonly used to store decimal values and perform mathematical calculations that require precision.

Characteristics of Float Type

  • Floats support decimal and exponential notation.
  • They occupy 8 bytes (64 bits) in memory.
  • Operations involving floats may lead to rounding errors due to binary representation limitations.

Example:

Python
a = 10.5  # Float declaration b = -3.14 # Negative float c = 2.0   # Even if it looks like an integer, it's a float d = 1.23e4  # Scientific notation (1.23 × 10⁴ = 12300.0) e = 5e-3   # Scientific notation (5 × 10⁻³ = 0.005) print(a,b,c,d,e) 

Built-in Methods for float type

Python provides several built-in methods for float objects.

Built-in Methods for float Type in Python

MethodDescription
float.as_integer_ratio()Returns a tuple representing the float as a ratio of two integers.
float.conjugate()Returns the same float value (useful for compatibility with complex numbers).
float.fromhex(s)Converts a hexadecimal string to a float. (Static method)
float.hex()Returns the hexadecimal representation of the float.
float.is_integer()Returns True if the float is an integer (has no decimal part), else False.
float.__abs__()Returns the absolute value of the float.
float.__add__(other)Adds two float values (self + other).
float.__sub__(other)Subtracts two float values (self – other).
float.__mul__(other)Multiplies two float values (self * other).
float.__truediv__(other)Performs true division (self / other).
float.__floordiv__(other)Performs floor division (self // other).
float.__mod__(other)Returns the remainder of division (self % other).
float.__pow__(other)Returns the float raised to the power of other (self ** other).
float.__round__(n)Rounds the float to n decimal places.

1. float.as_integer_ratio()

The as_integer_ratio() method returns a tuple of two integers, whose ratio equals the float. This method is useful for precise representation of floating-point numbers as fractions, which can help avoid floating-point precision errors in arithmetic calculations. The returned integers represent the numerator and denominator of the fraction.

Python
f = 2.75 ratio = f.as_integer_ratio() print(ratio)  

Output
(11, 4) 

Here, 2.75 is exactly represented as 11/4. The method breaks down the float into an exact fraction by multiplying it by a power of 2 internally and simplifying it into two integers.

2. float.conjugate()

The conjugate() method returns the same float value. This method exists primarily for compatibility with complex numbers, where the conjugate of a complex number negates its imaginary part. Since a float has no imaginary part, calling conjugate() on a float simply returns itself.

Python
f = 5.5 print(f.conjugate())   

Output
5.5 

This method does not modify the float; it just returns the same value. It ensures compatibility when working with complex numbers, where the conjugate of a + bi is a - bi.

3. float.fromhex(s)

The fromhex() method converts a hexadecimal string representation of a floating-point number into a float. This is useful when dealing with binary representations or low-level floating-point operations.

Python
s = "0x1.91eb851eb851fp+1" a = float.fromhex(s) print(a)   

Output
3.14 

The hexadecimal string represents a floating-point number in scientific notation. The p+1 denotes a power of two exponent. Converting from hexadecimal ensures precise representation of binary floating-point numbers.

4. float.hex()

The hex() method returns the hexadecimal representation of a float. This is useful for debugging floating-point precision issues and for storing exact binary representations.

Python
f = 3.14 print(f.hex())   

Output
0x1.91eb851eb851fp+1 

The output is a hexadecimal scientific notation representing the float. The p+1 means multiplying by 2^1. This format is useful for exact floating-point storage and computation.

5. float.is_integer()

The is_integer() method checks if a float has no decimal part and returns True if it is equivalent to an integer. This is useful when working with numerical computations where integer-like behavior is required.

Python
print((4.0).is_integer())   print((4.5).is_integer())   

Output
True False 

Here, 4.0 is equivalent to the integer 4, so is_integer() returns True. However, 4.5 has a decimal part, so it returns False.

6. float.__abs__()

The __abs__() method returns the absolute value of a float, which is the non-negative version of the number. It is equivalent to the built-in abs() function.

Python
f = -7.3 print(f.__abs__())   print(abs(f))        

Output
7.3 7.3 

The negative value -7.3 is converted to its positive equivalent 7.3. This is useful when working with distances, magnitudes, or other computations where only the positive value is needed.

7. float.__add__

The __add__() method performs addition between two float values. This is automatically used when we use the + operator.

Python
a = 5.5 b = 2.2 print(a.__add__(b))   print(a + b)         

Output
7.7 7.7 

Here, 5.5 + 2.2 results in 7.7. The + operator internally calls the __add__() method.

8. float.__sub__

The __sub__() method performs subtraction between two float values. It is used when we apply the - operator.

Python
a = 10.5 b = 3.2 print(a.__sub__(b))   print(a - b)          

Output
7.3 7.3 

Here, 10.5 - 3.2 results in 7.3. The - operator calls __sub__() internally.

9. float.__mul__(other)

The __mul__() method performs multiplication between two float values.

Python
a = 4.2 b = 2.0 print(a.__mul__(b))    print(a * b)           

Output
8.4 8.4 

Multiplication of 4.2 * 2.0 results in 8.4. The * operator invokes __mul__() internally.

10. float.__truediv__(other)

The __truediv__() method performs true division (returns a float even when dividing two integers).

Python
a = 7.5 b = 2.5 print(a.__truediv__(b))    print(a / b)               

Output
3.0 3.0 

The division 7.5 / 2.5 results in 3.0, ensuring a floating-point output.

11. float.__floordiv__(other)

The __floordiv__() method performs floor division, which returns the largest integer less than or equal to the quotient.

Python
a = 7.5 b = 2.5 print(a.__floordiv__(b))    print(a // b)               

Output
3.0 3.0 

The quotient is 3.0 with no remainder, so the floor division is the same as normal division here.

12. float.__mod__(other)

The __mod__() method returns the remainder of division.

Python
a = 10.5 b = 4.0 print(a.__mod__(b))    print(a % b)           

Output
2.5 2.5 

Here, 10.5 / 4.0 results in 2 with a remainder of 2.5.

13. float.__pow__(other)

The __pow__() method raises the float to the power of another number.

Python
a = 3.0 b = 2.0 print(a.__pow__(b))    print(a ** b)         

Output
9.0 9.0 

The expression 3.0 ** 2.0 calculates 3.0 raised to the power of 2.0, which is 9.0.

14. float.__round__(n)

The __round__() method rounds the float to n decimal places.

Python
f = 3.14159 print(f.__round__(2))   print(round(f, 2))       

Output
3.14 3.14 

The float 3.14159 is rounded to 3.14 when specifying 2 decimal places.



Next Article
Python | Decimal is_finite() method
author
aishwarya.27
Improve
Article Tags :
  • Python
  • python-basics
  • Python-Data Type
Practice Tags :
  • python

Similar Reads

  • Python | sympy.Float() method
    With the help of sympy.Float() method, we can convert the integer values to floating point values and by using this method we can also set the values of precision. By default value of precision is 15. Syntax : sympy.Float() Return : Return the floating point number. Example #1 : In this example we c
    1 min read
  • Define and Call Methods in a Python Class
    In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, prov
    3 min read
  • Python | Decimal is_finite() method
    Decimal#is_finite() : is_finite() is a Decimal class method which checks whether the Decimal value is finite value. Syntax: Decimal.is_finite() Parameter: Decimal values Return: true - if the Decimal value is finite value; otherwise false Code #1 : Example for is_finite() method # Python Program exp
    2 min read
  • Python | Decimal is_canonical() method
    Decimal#is_canonical() : is_canonical() is a Decimal class method which checks whether the Decimal value is canonical. Syntax: Decimal.is_canonical() Parameter: Decimal values Return: true - if the Decimal value is canonical; otherwise false Code #1 : Example for is_canonical() method # Python Progr
    2 min read
  • Python | Decimal from_float() method
    Decimal#from_float() : from_float() is a Decimal class method which converts a float to a decimal number, exactly. Syntax: Decimal.from_float() Parameter: Decimal values Return: converts a float to a decimal number, exactly. Code #1 : Example for from_float() method C/C++ Code # Python Program expla
    2 min read
  • Python | Decimal is_zero() method
    Decimal#is_zero() : is_zero() is a Decimal class method which checks whether the Decimal value is zero value. Syntax: Decimal.is_zero() Parameter: Decimal values Return: true - if the Decimal value is zero value; otherwise false Code #1 : Example for is_zero() method # Python Program explaining # is
    2 min read
  • Python List methods
    Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, we’ll explore all Python list methods with a simple example. List MethodsLet's look at different list methods in Python: append(): Adds a
    3 min read
  • Dunder or magic methods in Python
    Python Magic methods are the methods starting and ending with double underscores '__'. They are defined by built-in classes in Python and commonly used for operator overloading.  They are also called Dunder methods, Dunder here means "Double Under (Underscores)". Python Magic MethodsBuilt in classes
    7 min read
  • Emulating Numeric types in Python
    The following are the functions which can be defined to emulate numeric type objects. Methods to implement Binary operations on same type of objects : These functions will make no change in the calling object rather they will return a new numeric object of same type after performing certain operatio
    3 min read
  • Python | Decimal as_tuple() method
    Decimal#as_tuple() : as_tuple() is a Decimal class method which returns named tuple representation of the Decimal value. Syntax: Decimal.as_tuple() Parameter: Decimal values Return: tuple with values - sign - digits - exponent Code #1 : Example for as_tuple() method # Python Program explaining # as_
    2 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