将浮点数限制为小数点后两位

发布于 2024-07-12 22:49:13 字数 397 浏览 9 评论 0原文

我希望将 a 四舍五入为 13.95。 我尝试使用 round,但我得到:

>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999

对于标准库 Decimal 类的类似问题,请参阅如何格式化小数以始终显示 2 位小数?.

I want a to be rounded to 13.95. I tried using round, but I get:

>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999

For the analogous issue with the standard library Decimal class, see How can I format a decimal to always show 2 decimal places?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

黎歌 2024-07-19 22:49:13

您遇到了浮点数的老问题,即并非所有数字都可以准确表示。 命令行只是向您显示内存中的完整浮点形式。

对于浮点表示,您的舍入版本是相同的数字。 由于计算机是二进制的,它们将浮点数存储为整数,然后将其除以 2 的幂,因此 13.95 将以类似于 125650429603636838/(2**53) 的方式表示。

双精度数具有 53 位(16 位)精度,常规浮点数具有 24 位(8 位)精度。 Python 中的浮点类型使用双精度来存储值。

例如,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

如果您仅在小数点后两位(例如,为了显示货币值),那么您有几个更好的选择:

  1. 使用整数并以美分而不是美元存储值,然后除以 100 以转换为美元。
  2. 或者使用定点数,例如 十进制

You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
只是在用心讲痛 2024-07-19 22:49:13

有新的格式规范,字符串格式规范迷你语言

您可以执行相同的操作:

"{:.2f}".format(13.949999999999999)

注1:上面返回一个字符串。 为了获得浮动,只需用 float(...) 包装:

float("{:.2f}".format(13.949999999999999))

注 2:float() 包装不会改变任何事物:

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True

There are new format specifications, String Format Specification Mini-Language:

You can do the same as:

"{:.2f}".format(13.949999999999999)

Note 1: the above returns a string. In order to get as float, simply wrap with float(...):

float("{:.2f}".format(13.949999999999999))

Note 2: wrapping with float() doesn't change anything:

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
时光倒影 2024-07-19 22:49:13

内置的 round() 在 Python 2.7 或更高版本中工作得很好。

示例:

>>> round(14.22222223, 2)
14.22

查看文档

The built-in round() works just fine in Python 2.7 or later.

Example:

>>> round(14.22222223, 2)
14.22

Check out the documentation.

早乙女 2024-07-19 22:49:13

让我举一个 Python 3.6 的 f-string/template- 的例子字符串格式,我认为它非常简洁:

>>> f'{a:.2f}'

它也适用于较长的示例,带有运算符并且不需要括号:

>>> print(f'Completed in {time.time() - start:.2f}s')

Let me give an example in Python 3.6's f-string/template-string format, which I think is beautifully neat:

>>> f'{a:.2f}'

It works well with longer examples too, with operators and not needing parentheses:

>>> print(f'Completed in {time.time() - start:.2f}s')
燃情 2024-07-19 22:49:13

我觉得最简单的方法是使用 format() 函数。

例如:

a = 13.949999999999999
format(a, '.2f')

13.95

这会生成一个浮点数作为四舍五入到小数点后两位的字符串。

I feel that the simplest approach is to use the format() function.

For example:

a = 13.949999999999999
format(a, '.2f')

13.95

This produces a float number as a string rounded to two decimal points.

来日方长 2024-07-19 22:49:13

大多数数字无法用浮点数精确表示。 如果您想对数字进行舍入,因为这是您的数学公式或算法所需要的,那么您需要使用舍入。 如果您只想将显示限制为一定的精度,那么甚至不要使用 round 并将其格式化为该字符串。 (如果您想使用某种替代舍入方法来显示它,并且有很多舍入方法,那么您需要混合使用这两种方法。)

>>> "%.2f" % 3.14159
'3.14'
>>> "%.2f" % 13.9499999
'13.95'

最后,也许最重要的是,如果您想要精确数学,那么您根本不想要漂浮物。 通常的例子是处理金钱并将“分”存储为整数。

Most numbers cannot be exactly represented in floats. If you want to round the number because that's what your mathematical formula or algorithm requires, then you want to use round. If you just want to restrict the display to a certain precision, then don't even use round and just format it as that string. (If you want to display it with some alternate rounding method, and there are tons, then you need to mix the two approaches.)

>>> "%.2f" % 3.14159
'3.14'
>>> "%.2f" % 13.9499999
'13.95'

And lastly, though perhaps most importantly, if you want exact math then you don't want floats at all. The usual example is dealing with money and to store 'cents' as an integer.

随波逐流 2024-07-19 22:49:13

使用

print"{:.2f}".format(a)

而不是

print"{0:.2f}".format(a)

因为后者在尝试输出多个变量时可能会导致输出错误(请参阅注释)。

Use

print"{:.2f}".format(a)

instead of

print"{0:.2f}".format(a)

Because the latter may lead to output errors when trying to output multiple variables (see comments).

懒猫 2024-07-19 22:49:13

TLDR ;)

输入和输出的舍入问题已由 Python 3.1 明确解决,并且该修复也向后移植到 Python 2.7.0。

四舍五入数字可以在浮点数和字符串之间可逆转换
str-> 浮动() - > repr() ->; float() ...十进制 -> 浮动-> str-> Decimal

>>> 0.3
0.3
>>> float(repr(0.3)) == 0.3
True

存储不再需要 Decimal 类型。

算术运算的结果必须再次四舍五入,因为四舍五入错误可能会累积比解析一个数字后可能出现的错误更多的错误。 改进的 repr() 算法无法解决这个问题(Python >= 3.1,>= 2.7.0):

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)

输出字符串函数 str(float(...))< /code> 在 Python 中四舍五入为 12 位有效数字 < 2.7 倍且 < 3.1,防止过多的无效数字,类似于未固定的 repr() 输出。 在减去非常相似的数字之后,这仍然是不够的,并且在其他操作之后它又被舍入太多了。 Python 2.7 和 3.1 使用相同长度的 str(),尽管 repr() 是固定的。 一些旧版本的 Numpy 也存在过多的无效数字,即使使用固定的 Python 也是如此。 当前的 Numpy 是固定的。 Python 版本 >= 3.2 具有相同的 str() 和 repr() 函数结果以及 Numpy 中类似函数的输出。


测试

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.

文档

请参阅发行说明 Python 2.7 - 其他语言更改 第四段:

浮点数和字符串之间的转换现在在大多数平台上都正确舍入。 这些转换发生在许多不同的地方:str() 浮点数和复数; 浮点数和复杂构造函数; 数字格式; 使用 marshalpicklejson 模块序列化和反序列化浮点数和复数; Python 代码中浮点和虚数文字的解析; 和十进制到浮点的转换。

与此相关的是,浮点数 x 的 repr() 现在会根据保证四舍五入到 x 的最短十进制字符串返回结果正确的舍入(使用半舍入到偶数舍入模式)。 以前,它给出了一个基于将 x 四舍五入到 17 位十进制数字的字符串。

相关问题


更多信息: float Python 2.7 之前的版本与当前的 numpy.float64 类似。 两种类型都使用相同的 64 位 IEEE 754 双精度和 52 位尾数。 一个很大的区别是 np.float64.__repr__ 经常使用过多的十进制数字进行格式化,以便不会丢失任何位,但在 13.9499999999999999 和 13.950000000000001 之间不存在有效的 IEEE 754 数字。 结果并不好,并且 numpy 的转换 repr(float(number_as_string)) 不可逆。 另一方面:float.__repr__ 的格式使得每个数字都很重要; 序列没有间隙并且转换是可逆的。 简单地说:如果您可能有一个 numpy.float64 数字,请将其转换为普通浮点数,以便为人类而不是数字处理器格式化,否则 Python 2.7+ 不需要更多。

TLDR ;)

The rounding problem of input and output has been solved definitively by Python 3.1 and the fix is backported also to Python 2.7.0.

Rounded numbers can be reversibly converted between float and string back and forth:
str -> float() -> repr() -> float() ... or Decimal -> float -> str -> Decimal

>>> 0.3
0.3
>>> float(repr(0.3)) == 0.3
True

A Decimal type is not necessary for storage anymore.

Results of arithmetic operations must be rounded again because rounding errors could accumulate more inaccuracy than that is possible after parsing one number. That is not fixed by the improved repr() algorithm (Python >= 3.1, >= 2.7.0):

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)

The output string function str(float(...)) was rounded to 12 valid digits in Python < 2.7x and < 3.1, to prevent excessive invalid digits similar to unfixed repr() output. That was still insufficientl after subtraction of very similar numbers and it was too much rounded after other operations. Python 2.7 and 3.1 use the same length of str() although the repr() is fixed. Some old versions of Numpy had also excessive invalid digits, even with fixed Python. The current Numpy is fixed. Python versions >= 3.2 have the same results of str() and repr() function and also output of similar functions in Numpy.


Test

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.

Documentation

See the Release notes Python 2.7 - Other Language Changes the fourth paragraph:

Conversions between floating-point numbers and strings are now correctly rounded on most platforms. These conversions occur in many different places: str() on floats and complex numbers; the float and complex constructors; numeric formatting; serializing and de-serializing floats and complex numbers using the marshal, pickle and json modules; parsing of float and imaginary literals in Python code; and Decimal-to-float conversion.

Related to this, the repr() of a floating-point number x now returns a result based on the shortest decimal string that’s guaranteed to round back to x under correct rounding (with round-half-to-even rounding mode). Previously it gave a string based on rounding x to 17 decimal digits.

The related issue


More information: The formatting of float before Python 2.7 was similar to the current numpy.float64. Both types use the same 64 bit IEEE 754 double precision with 52 bit mantissa. A big difference is that np.float64.__repr__ is formatted frequently with an excessive decimal number so that no bit can be lost, but no valid IEEE 754 number exists between 13.949999999999999 and 13.950000000000001. The result is not nice and the conversion repr(float(number_as_string)) is not reversible with numpy. On the other hand: float.__repr__ is formatted so that every digit is important; the sequence is without gaps and the conversion is reversible. Simply: If you perhaps have a numpy.float64 number, convert it to normal float in order to be formatted for humans, not for numeric processors, otherwise nothing more is necessary with Python 2.7+.

寄人书 2024-07-19 22:49:13

尝试下面的代码:

>>> a = 0.99334
>>> a = int((a * 100) + 0.5) / 100.0 # Adding 0.5 rounds it up
>>> print a
0.99

Try the code below:

>>> a = 0.99334
>>> a = int((a * 100) + 0.5) / 100.0 # Adding 0.5 rounds it up
>>> print a
0.99
奢欲 2024-07-19 22:49:13

使用:

float_number = 12.234325335563
round(float_number, 2)

这将返回;

12.23

说明:

round函数有两个参数;
要四舍五入的数字和要返回的小数位数。 这里我返回了两位小数。

Use:

float_number = 12.234325335563
round(float_number, 2)

This will return;

12.23

Explanation:

The round function takes two arguments;
The number to be rounded and the number of decimal places to be returned. Here I returned two decimal places.

番薯 2024-07-19 22:49:13

您可以修改输出格式:

>>> a = 13.95
>>> a
13.949999999999999
>>> print "%.2f" % a
13.95

You can modify the output format:

>>> a = 13.95
>>> a
13.949999999999999
>>> print "%.2f" % a
13.95
梦回梦里 2024-07-19 22:49:13

与Python< 3(例如2.6或2.7),有两种方法可以做到这一点。

# Option one 
older_method_string = "%.9f" % numvar

# Option two (note ':' before the '.9f')
newer_method_string = "{:.9f}".format(numvar)

但请注意,对于 3 以上的 Python 版本(例如 3.2 或 3.3),选项二是 首选

有关选项二的更多信息,我建议此链接位于 Python 文档中的字符串格式< /a>.

有关选项一的更多信息,此链接就足够了,并且包含有关各种标志

参考:将浮点数转换为一定的精度,然后复制到字符串

With Python < 3 (e.g. 2.6 or 2.7), there are two ways to do so.

# Option one 
older_method_string = "%.9f" % numvar

# Option two (note ':' before the '.9f')
newer_method_string = "{:.9f}".format(numvar)

But note that for Python versions above 3 (e.g. 3.2 or 3.3), option two is preferred.

For more information on option two, I suggest this link on string formatting from the Python documentation.

And for more information on option one, this link will suffice and has information on the various flags.

Reference: Convert floating point number to a certain precision, and then copy to string

洛阳烟雨空心柳 2024-07-19 22:49:13

您可以使用 format 运算符将值四舍五入到小数点后两位在Python中:

print(format(14.4499923, '.2f')) // The output is 14.45

You can use format operator for rounding the value up to two decimal places in Python:

print(format(14.4499923, '.2f')) // The output is 14.45
浮云落日 2024-07-19 22:49:13

正如 Matt 指出的,Python 3.6 提供了f-strings,并且它们还可以使用 嵌套参数

value = 2.34558
precision = 2
width = 4

print(f'result: {value:{width}.{precision}f}')

将显示结果:2.35

As Matt pointed out, Python 3.6 provides f-strings, and they can also use nested parameters:

value = 2.34558
precision = 2
width = 4

print(f'result: {value:{width}.{precision}f}')

which will display result: 2.35

入画浅相思 2024-07-19 22:49:13

在 Python 2.7 中:

a = 13.949999999999999
output = float("%0.2f"%a)
print output

In Python 2.7:

a = 13.949999999999999
output = float("%0.2f"%a)
print output
友欢 2024-07-19 22:49:13

我们有多种选择来做到这一点:

选项 1:

x = 1.090675765757
g = float("{:.2f}".format(x))
print(g)

选项 2:
内置的 round() 支持 Python 2.7 或更高版本。

x = 1.090675765757
g = round(x, 2)
print(g)

We multiple options to do that:

Option 1:

x = 1.090675765757
g = float("{:.2f}".format(x))
print(g)

Option 2:
The built-in round() supports Python 2.7 or later.

x = 1.090675765757
g = round(x, 2)
print(g)
凑诗 2024-07-19 22:49:13

Python 教程有一个名为浮点算术:问题和限制。 阅读。 它解释了正在发生的事情以及为什么 Python 会尽力而为。 它甚至还有一个与您的示例相匹配的示例。 让我引用一下:

<前><代码>>>> 0.1
0.10000000000000001

您可能会想使用round()
函数将其切回单个
您期望的数字。 但这使得不
区别:

<前><代码>>>> 回合(0.1, 1)
0.10000000000000001

问题是二进制文件
“0.1”存储的浮点值
已经是最好的二进制文件
近似于 1/10,所以尝试
再次四舍五入不能让它变得更好:
它已经是最好的了。

另一个结果是,自从0.1
不完全是 1/10,十之和
0.1 的值可能无法准确产生
1.0,或者:

<前><代码>>>> 总和 = 0.0
>>>>> 对于范围 (10) 内的 i:
... 总和 += 0.1
...
>>>>> 和
0.99999999999999989

解决问题的一种替代方法和解决方案是使用 <代码>十进制模块。

The Python tutorial has an appendix called Floating Point Arithmetic: Issues and Limitations. Read it. It explains what is happening and why Python is doing its best. It has even an example that matches yours. Let me quote a bit:

>>> 0.1
0.10000000000000001

you may be tempted to use the round()
function to chop it back to the single
digit you expect. But that makes no
difference:

>>> round(0.1, 1)
0.10000000000000001

The problem is that the binary
floating-point value stored for “0.1”
was already the best possible binary
approximation to 1/10, so trying to
round it again can’t make it better:
it was already as good as it gets.

Another consequence is that since 0.1
is not exactly 1/10, summing ten
values of 0.1 may not yield exactly
1.0, either:

>>> sum = 0.0
>>> for i in range(10):
...     sum += 0.1
...
>>> sum
0.99999999999999989

One alternative and solution to your problems would be using the decimal module.

喜爱皱眉﹌ 2024-07-19 22:49:13

使用 Decimal 对象和 round() 方法的组合。

Python 3.7.3
>>> from decimal import Decimal
>>> d1 = Decimal (13.949999999999999) # define a Decimal
>>> d1 
Decimal('13.949999999999999289457264239899814128875732421875')
>>> d2 = round(d1, 2) # round to 2 decimals
>>> d2
Decimal('13.95')

Use combination of Decimal object and round() method.

Python 3.7.3
>>> from decimal import Decimal
>>> d1 = Decimal (13.949999999999999) # define a Decimal
>>> d1 
Decimal('13.949999999999999289457264239899814128875732421875')
>>> d2 = round(d1, 2) # round to 2 decimals
>>> d2
Decimal('13.95')
離殇 2024-07-19 22:49:13

对于原始 Python:

对于浮点到字符串转换 2 位小数:

a = 13.949999999999999
format(a, '.2f')

对于浮点到浮点转换 2 位小数:

a = 13.949999999999999
round(float(a), 2)
or
float(format(a, '.2f'))

For Raw Python:

For float to string converted 2 decimal points:

a = 13.949999999999999
format(a, '.2f')

For float to float converted 2 decimal points:

a = 13.949999999999999
round(float(a), 2)
or
float(format(a, '.2f'))
吻风 2024-07-19 22:49:13

它完全按照您的指示进行操作并且工作正常。 阅读有关浮点混淆的更多信息,也许可以尝试decimal 对象代替。

It's doing exactly what you told it to do and is working correctly. Read more about floating point confusion and maybe try decimal objects instead.

不交电费瞎发啥光 2024-07-19 22:49:13
from decimal import Decimal


def round_float(v, ndigits=2, rt_str=False):
    d = Decimal(v)
    v_str = ("{0:.%sf}" % ndigits).format(round(d, ndigits))
    if rt_str:
        return v_str
    return Decimal(v_str)

结果:

Python 3.6.1 (default, Dec 11 2018, 17:41:10)
>>> round_float(3.1415926)
Decimal('3.14')
>>> round_float(3.1445926)
Decimal('3.14')
>>> round_float(3.1455926)
Decimal('3.15')
>>> round_float(3.1455926, rt_str=True)
'3.15'
>>> str(round_float(3.1455926))
'3.15'
from decimal import Decimal


def round_float(v, ndigits=2, rt_str=False):
    d = Decimal(v)
    v_str = ("{0:.%sf}" % ndigits).format(round(d, ndigits))
    if rt_str:
        return v_str
    return Decimal(v_str)

Results:

Python 3.6.1 (default, Dec 11 2018, 17:41:10)
>>> round_float(3.1415926)
Decimal('3.14')
>>> round_float(3.1445926)
Decimal('3.14')
>>> round_float(3.1455926)
Decimal('3.15')
>>> round_float(3.1455926, rt_str=True)
'3.15'
>>> str(round_float(3.1455926))
'3.15'
一念一轮回 2024-07-19 22:49:13

使用像这样的 lambda 函数:

arred = lambda x,n : x*(10**n)//1/(10**n)

这样你就可以这样做:

arred(3.141591657, 2)

并得到

3.14

Use a lambda function like this:

arred = lambda x,n : x*(10**n)//1/(10**n)

This way you could just do:

arred(3.141591657, 2)

and get

3.14
戒ㄋ 2024-07-19 22:49:13

它很简单,例如:

  1. 使用 decimal 模块快速正确舍入小数浮点运算:

    <预置><代码> d = 十进制(10000000.0000009)

    实现四舍五入:

     d.quantize(Decimal('0.01')) 
      

    结果为Decimal('10000000.00')

  2. 使上述内容 干燥

    def round_decimal(number, exponent='0.01'): 
          小数值 = 小数(数字) 
          返回decimal_value.quantize(小数(指数)) 
      

    def round_decimal(数字,decimal_places = 2): 
          小数值 = 小数(数字) 
          返回decimal_value.quantize(Decimal(10) ** -decimal_places) 
      

PS:对他人的批评: 格式不四舍五入。

It's simple like:

  1. use decimal module for fast correctly-rounded decimal floating point arithmetic:

     d = Decimal(10000000.0000009)
    

    to achieve rounding:

     d.quantize(Decimal('0.01'))
    

    will result with Decimal('10000000.00')

  2. make the above DRY:

    def round_decimal(number, exponent='0.01'):
        decimal_value = Decimal(number)
        return decimal_value.quantize(Decimal(exponent))
    

    or

    def round_decimal(number, decimal_places=2):
        decimal_value = Decimal(number)
        return decimal_value.quantize(Decimal(10) ** -decimal_places)
    

PS: critique of others: formatting is not rounding.

农村范ル 2024-07-19 22:49:13

简单的解决方案就在这里

value = 5.34343
rounded_value = round(value, 2) # 5.34

The simple solution is here

value = 5.34343
rounded_value = round(value, 2) # 5.34
错爱 2024-07-19 22:49:13
orig_float = 232569 / 16000.0

14.5355625

short_float = float("{:.2f}".format(orig_float)) 

14.54

orig_float = 232569 / 16000.0

14.5355625

short_float = float("{:.2f}".format(orig_float)) 

14.54

薄暮涼年 2024-07-19 22:49:13

为了修复 Python 和 JavaScript 等动态类型语言中的浮点,我使用这种技术

# For example:
a = 70000
b = 0.14
c = a * b

print c # Prints 980.0000000002
# Try to fix
c = int(c * 10000)/100000
print c # Prints 980

您也可以使用 Decimal,如下所示:

from decimal import *
getcontext().prec = 6
Decimal(1) / Decimal(7)
# Results in 6 precision -> Decimal('0.142857')

getcontext().prec = 28
Decimal(1) / Decimal(7)
# Results in 28 precision -> Decimal('0.1428571428571428571428571429')

For fixing the floating point in type-dynamic languages such as Python and JavaScript, I use this technique

# For example:
a = 70000
b = 0.14
c = a * b

print c # Prints 980.0000000002
# Try to fix
c = int(c * 10000)/100000
print c # Prints 980

You can also use Decimal as following:

from decimal import *
getcontext().prec = 6
Decimal(1) / Decimal(7)
# Results in 6 precision -> Decimal('0.142857')

getcontext().prec = 28
Decimal(1) / Decimal(7)
# Results in 28 precision -> Decimal('0.1428571428571428571428571429')
寄风 2024-07-19 22:49:13

这是使用格式的简单解决方案 功能。

float(format(num, '.2f'))

注意:我们将数字转换为浮点数,因为 format 方法返回一个字符串。

Here is the simple solution using the format function.

float(format(num, '.2f'))

Note: We are converting numbers to float, because the format method is returning a string.

毁梦 2024-07-19 22:49:13

如果你想处理金钱,请使用 Python decimal 模块:

from decimal import Decimal, ROUND_HALF_UP

# 'amount' can be integer, string, tuple, float, or another Decimal object
def to_money(amount) -> Decimal:
    money = Decimal(amount).quantize(Decimal('.00'), rounding=ROUND_HALF_UP)
    return money

If you want to handle money, use the Python decimal module:

from decimal import Decimal, ROUND_HALF_UP

# 'amount' can be integer, string, tuple, float, or another Decimal object
def to_money(amount) -> Decimal:
    money = Decimal(amount).quantize(Decimal('.00'), rounding=ROUND_HALF_UP)
    return money
池予 2024-07-19 22:49:13
lambda x, n:int(x*10^n + 0.5)/10^n

为我工作了很多年,使用多种语言。

lambda x, n:int(x*10^n + 0.5)/10^n

has worked for me for many years in many languages.

九歌凝 2024-07-19 22:49:13

要将数字舍入为分辨率,最好的方法是以下方法,它可以使用任何分辨率(0.01 表示两位小数,甚至其他步长):

>>> import numpy as np
>>> value = 13.949999999999999
>>> resolution = 0.01
>>> newValue = int(np.round(value/resolution))*resolution
>>> print newValue
13.95

>>> resolution = 0.5
>>> newValue = int(np.round(value/resolution))*resolution
>>> print newValue
14.0

To round a number to a resolution, the best way is the following one, which can work with any resolution (0.01 for two decimals or even other steps):

>>> import numpy as np
>>> value = 13.949999999999999
>>> resolution = 0.01
>>> newValue = int(np.round(value/resolution))*resolution
>>> print newValue
13.95

>>> resolution = 0.5
>>> newValue = int(np.round(value/resolution))*resolution
>>> print newValue
14.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文