If you'd prefer just an integer number of cents:
cents_int = int(round(float(dollars.strip('$'))*100))
If you want a Decimal, just use...
from decimal import Decimal
dollars_dec = Decimal(dollars.strip('$'))
If you know that the dollar sign will always be there, you could use dollars[1:]
instead of dollars.strip('$')
, but using strip()
lets you also handle strings that omit the dollar sign (5.99
instead of $5.99
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…