当前位置:  开发笔记 > 编程语言 > 正文

Python float __setformat__?

如何解决《Pythonfloat__setformat__?》经验,为你挑选了1个好方法。

我有一个问题,我需要得到一个正确的浮点数小数点后18位.当我使用默认浮点数(数字)时,它只给我12个小数位.

然后我做了dir(浮动)

    ['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__',
 '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__',
 '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__m
od__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '
__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '_
_rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__s
tr__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', '
fromhex', 'hex', 'imag', 'is_integer', 'real']

该块中有一些叫做__setformat__的东西.有什么用?以及如何使用它进行浮点精度设置?

我使用的是python 2.7.5 x64.



1> Martijn Piet..:

它只适用于Python测试套件; help(float.__setformat__)打印:

float.__setformat__(typestr, fmt) -> None

您可能不想使用此功能.它主要存在于Python的测试套件中.

typestr必须是'double''float'. 如果fmt看起来与底层C现实相匹配,则fmt必须是'unknown', 'IEEE, big-endian'或者'IEEE, little-endian',并且此外只能是后两者之一.

覆盖C级浮点类型的自动确定.这会影响浮点数与二进制字符串的转换方式.

还有一个float.__getformat__()相同信息,相同目的的getter方法.

有关其使用的更多详细信息,请参阅float测试套件.

使用该decimal模块可以获得更准确的十进制计算,但请查看sys.float_info有关平台上精确浮动的详细信息.例如,我的64位Mac OS X系统只能管理15位数字.

推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有