Showing posts with label float. Show all posts
Showing posts with label float. Show all posts
How to Convert tuple to float value in python - Handy Hack
1 comment
:
Posted by:
Handy Hack
at
April 30, 2020
Tags:
float
,
handy hack
,
program
,
python
,
tuple
Many times, When we working with tuple, we have faced a problem in which we need to convert a tuple to floating point number. In a tuple first element represents integer part and next element represents a decimal part. Now the problem is - How to Convert tuple to float value? Let's discuss the creation way-
Main Method: Using join() + float() + str() + generator expression
Using this method we just convert first tuple element into a string, and then join them and convert them to desire integer.
Source Code:
# Convert tuple to float
# using join() + float() + str() + generator expression
#initialize tuple
tuple = (9,12)
#printing original tuple
print("The original tuple: " + str(tuple))
# Convert tuple to float
# using join() + float() + str() + generator expression
var = float(' . ' . join(str(ele) for ele in tuple))
#print result
print("The float number after conversion from tuple is : " + str(var))
Output:
The original tuple: (9,12)
The float number after conversion from tuple is : 9.12
Note:
1. Concatenate a list of strings into one string: join()
2. Concatenate a list of numbers into one string: join(), str()
Main Method: Using join() + float() + str() + generator expression
Using this method we just convert first tuple element into a string, and then join them and convert them to desire integer.
Source Code:
# Convert tuple to float
# using join() + float() + str() + generator expression
#initialize tuple
tuple = (9,12)
#printing original tuple
print("The original tuple: " + str(tuple))
# Convert tuple to float
# using join() + float() + str() + generator expression
var = float(' . ' . join(str(ele) for ele in tuple))
#print result
print("The float number after conversion from tuple is : " + str(var))
Output:
The original tuple: (9,12)
The float number after conversion from tuple is : 9.12
Note:
1. Concatenate a list of strings into one string: join()
2. Concatenate a list of numbers into one string: join(), str()
Subscribe to:
Posts
(
Atom
)
