如何从字符串“$61.52”中提取整数以用于Python中的算术运算? [重复]

问题描述 投票:0回答:1

我正在使用Python。 如何将字符串

$61.52
$57.74
相加得到
119.26
的值?

我尝试使用

isdigit()
但它提取数字为
6152
而不是
61.52
。我想从字符串
61.52
中提取
$61.52

python type-conversion
1个回答
0
投票

我会做什么:

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

result = amount1 + amount2

print(f'The sum is: ${result:.2f}')
© www.soinside.com 2019 - 2024. All rights reserved.