Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to convert number string or fraction to float

from fractions import Fraction


def number_str_to_float(amount_str):
    number_as_float = amount_str
    try:
        number_as_float = round(float(sum(Fraction(s)
                                for s in f"{amount_str}".split())), 2)
    except:
        print(f''{number_as_float}' is not a number or fraction')
    if isinstance(number_as_float, float):
        print(number_as_float)


number_str_to_float("5 1/3")

# Output
# 5.33
Comment

PREVIOUS NEXT
Code Example
Python :: df iterrows pandas 
Python :: convert column to datetime format python 
Python :: python clean recycle bin 
Python :: dataframe to csv python 
Python :: rotate screen trick in python 
Python :: hwo much does mano house cost in python 
Python :: get all environment variables python 
Python :: mac install python 3.8 
Python :: how to take list of integer as input in python 
Python :: how to delete every row in excel using openpyxl 
Python :: timeout exception in selenium python 
Python :: python typing as int or float 
Python :: remove extension from filename python 
Python :: how to identify GPU with pytorch script 
Python :: convert negative to zero in list in python 
Python :: beuatiful soup find a href 
Python :: hwo to separate datetime column into date and time pandas 
Python :: dataframe get list of index vlaues 
Python :: python auto clicker 
Python :: rename df column 
Python :: install pipenv on windows 
Python :: pandas convert index to column 
Python :: plural name django 
Python :: how to set learning rate in keras 
Python :: pretty print pandas dataframe 
Python :: correlation between lists python 
Python :: how to lowercase list in python 
Python :: print current time hours and minutes in python 
Python :: make first row columns pandas 
Python :: python find all pairs in list 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =