如何从字符串 '$61.52' 中提取整数以在Python中进行算术运算?

0 投票
1 回答
24 浏览
提问于 2025-04-14 18:29

我在使用Python。
我想把字符串 $61.52$57.74 相加,得到的结果是 119.26,该怎么做呢?

我试过使用 isdigit(),但它提取出来的数字是 6152,而不是 61.52。我想从字符串 $61.52 中提取出 61.52

1 个回答

1

我会这样做:

amount1 = float('$61.52'.replace('$', ''))
amount2 = float('$57.74'.replace('$', ''))

result = amount1 + amount2

print(f'The sum is: ${result:.2f}')

撰写回答