python float 精度,将Python Float转换为String而不会丢失精度

博主在维护一个使用xlrd从Excel获取高精度数值的Python脚本,遇到将浮点数转换为字符串时丢失精度的问题。尝试了字符串格式化和Decimal类,但未能成功。解决方案是使用`repr()`函数,它能保证浮点数转换为字符串后,再转换回浮点数时精度不变。
摘要由CSDN通过智能技术生成

I am maintaining a Python script that uses xlrd to retrieve values from Excel spreadsheets, and then do various things with them. Some of the cells in the spreadsheet are high-precision numbers, and they must remain as such. When retrieving the values of one of these cells, xlrd gives me a float such as 0.38288746115497402.

However, I need to get this value into a string later on in the code. Doing either str(value) or unicode(value) will return something like "0.382887461155". The requirements say that this is not acceptable; the precision needs to be preserved.

I've tried a couple things so far to no success. The first was using a string formatting thingy:

data = "%.40s" % (value)

data2 = "%.40r" % (value)

But both produce the same rounded number, "0.382887461155".

Upon searching around for people with simil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值