Here is the simple solution with Python:
Using the while loop and if...else conditions to check specific string pattern and replace unwanted character.
while
if...else
Then return s with casting to float value finally.
s
def money_value(s): s = s.replace(' ', '') s = s.replace('$' , '') if s == '': return 0.0 if s[0] == '.': s = '0' + s if s[0] == '0' and s[1] == '0': s = s.replace('0', '') if len(s) < 4: if '.' not in s: s += '.' start_length = 0 zero_len = 4 - len(s) while start_length < zero_len: s += '0' start_length += 1 return float(s)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here is the simple solution with Python:
Using the
while
loop andif...else
conditions to check specific string pattern and replace unwanted character.Then return
s
with casting to float value finally.