如何将浮点数作为文本存储而不会丢失精度? [英] How to store a floating point number as text without losing precision?

查看:198
本文介绍了如何将浮点数作为文本存储而不会丢失精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像问题所说的那样。转换为(截断的)字符串表示形式可能会影响其精度。但是将它们存储为其他格式,如pickle使它们无法读取(是的,我也想这样做)。

如何在浮点数中保存浮点数而不丢失精度?解决方案我建议使用内置函数 repr()。从文档:


repr(object) - > string

返回该对象的规范字符串表示形式。
对于大多数对象类型,eval(repr(object))==对象。


Like the question says. Converting to / from the (truncated) string representations can affect their precision. But storing them in other formats like pickle makes them unreadable (yes, I want this too).

How can I store floating point numbers in text without losing precision?

解决方案

I'd suggest using the builtin function repr(). From the documentation:

repr(object) -> string

Return the canonical string representation of the object. For most object types, eval(repr(object)) == object.

这篇关于如何将浮点数作为文本存储而不会丢失精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆