Home » Python

float() function with example in Python

Python float() function: Here, we are going to learn about the float() function in Python with example.
Submitted by IncludeHelp, on April 02, 2019

Python float() function

float() function is a library function in Python, it is used to get a float value from a given number or a string. It accepts either a string (that should contain a number) or a number and returns float value.

Syntax:

    float(value)

Parameter:value – string or number to be converted into float.

Return value: float – returns a float value.

Example:

    Input:
    val1 = "10.23"
    val2 = "10"
    val3 = 10
    print(float(val1))
    print(float(val2))
    print(float(val3))
    
    Output:
    10.23
    10.0
    10.0

Python code to convert string or number into float

# python code to demonstrate an example
# of float() function

print(float("10.23"))   # will return 10.23
print(float("10"))      # will return 10.0
print(float(10))        # will return 10.0
print(float(10.23))     # will return 10.23

Output

10.23
10.0
10.0
10.23





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.