浮点数以2位小数浮点数?

内克斯拉

我有一个功能

def float_():
    price_data = {}
    str_num = '3.505,32'
    price_data['price'] = float(str_num.replace('.','').replace(',','.'))
    price_data['currency'] = 'USD'
    return price_data

有没有其他方法可以实现这个浮动结果?

马修斯·波特拉

您可以使用locale.atof,如本问题所示,它抽象了诸如十进制标点符号和千位分隔符之类的内容:

import locale
locale.setlocale(locale.LC_ALL, 'pt_BR') # Set to the proper language/country

def float_():
    price_data = {}
    str_num = '3.505,32'
    price_data['price'] = locale.atof(str_num)
    price_data['currency'] = 'USD'
    return price_data

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章