简体   繁体   English

如何将浮点数四舍五入到某个小数位?

[英]How to round a floating point number up to a certain decimal place?

Suppose I have 8.8333333333333339 , and I want to convert it to 8.84 .假设我有8.8333333333333339 ,我想将其转换为8.84 How can I accomplish this in Python?如何在 Python 中完成此操作?

round(8.8333333333333339, 2) gives 8.83 and not 8.84 . round(8.8333333333333339, 2)给出8.83而不是8.84 I am new to Python or programming in general.我是 Python 或一般编程的新手。

I don't want to print it as a string, and the result will be further used.我不想将其打印为字符串,结果将进一步使用。 For more information on the problem, please check Tim Wilson's Python Programming Tips: Loan and payment calculator .有关该问题的更多信息,请查看Tim Wilson 的 Python 编程技巧:贷款和付款计算器

8.833333333339 (or 8.833333333333334 , the result of 106.00/12 ) properly rounded to two decimal places is 8.83 . 8.833333333339 (或8.833333333333334 106.00/12的结果)正确四舍五入到两位小数是8.83 Mathematically it sounds like what you want is a ceiling function .从数学上讲,这听起来像是您想要的天花板函数 The one in Python's math module is named ceil : Python 的math模块中的一个名为ceil

import math

v = 8.8333333333333339
print(math.ceil(v*100)/100)  # -> 8.84

Respectively, the floor and ceiling functions generally map a real number to the largest previous or smallest following integer which has zero decimal places — so to use them for 2 decimal places the number is first multiplied by 10 2 (or 100) to shift the decimal point and is then divided by it afterwards to compensate.分别地,地板和天花板函数通常将实数映射到最大的前一个或最小的后一个整数,其中小数位为零 - 因此要将它们用于 2 个小数位,数字首先乘以 10 2 (或 100)以移动小数点,然后除以它来补偿。

If you don't want to use the math module for some reason, you can use this (minimally tested) implementation I just wrote:如果您出于某种原因不想使用math模块,您可以使用我刚刚写的这个(经过最少测试的)实现:

def ceiling(x):
    n = int(x)
    return n if n-1 < x <= n else n+1

How all this relates to the linked Loan and payment calculator problem:所有这些与链接的贷款和付款计算器问题有何关系:

贷款计算器输出截图

From the sample output it appears that they rounded up the monthly payment, which is what many call the effect of the ceiling function.从样本输出看来,他们将每月付款四舍五入,这就是许多人所说的上限函数的效果。 This means that each month a little more than 112 of the total amount is being paid.这意味着每个月支付的总金额略高于112 That made the final payment a little smaller than usual — leaving a remaining unpaid balance of only 8.76 .这使得最后的付款比平时少了一点——剩下的未付余额只有8.76

It would have been equally valid to use normal rounding producing a monthly payment of 8.83 and a slightly higher final payment of 8.87 .使用正常四舍五入产生每月付款8.83和略高的最终付款8.87将同样有效。 However, in the real world people generally don't like to have their payments go up, so rounding up each payment is the common practice — it also returns the money to the lender more quickly.然而,在现实世界中,人们通常不喜欢他们的付款增加,因此将每笔付款四舍五入是常见的做法 - 它还可以更快地将钱退还给贷方。

This is normal (and has nothing to do with Python) because 8.83 cannot be represented exactly as a binary float, just as 1/3 cannot be represented exactly in decimal (0.333333... ad infinitum). 这是正常的(与 Python 无关),因为 8.83 不能完全表示为二进制浮点数,正如 1/3 不能完全表示为十进制(0.333333...无限期)。

If you want to ensure absolute precision, you need the decimal module:如果要确保绝对精度,则需要decimal模块:

>>> import decimal
>>> a = decimal.Decimal("8.833333333339")
>>> print(round(a,2))
8.83

You want to use the decimal module but you also need to specify the rounding mode.您想使用小数模块,但您还需要指定舍入模式。 Here's an example:下面是一个例子:

>>> import decimal
>>> decimal.Decimal('8.333333').quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_UP)
Decimal('8.34')
>>> decimal.Decimal('8.333333').quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_DOWN)
Decimal('8.33')
>>> 

A much simpler way is to simply use the round() function.一个更简单的方法是简单地使用 round() 函数。 Here is an example.这是一个例子。

total_price = float()
price_1 = 2.99
price_2 = 0.99
total_price = price_1 + price_2

If you were to print out total_price right now you would get如果你现在打印 total_price,你会得到

3.9800000000000004

But if you enclose it in a round() function like so但是如果你像这样将它包含在一个 round() 函数中

print(round(total_price,2))

The output equals输出等于

3.98

The round() function works by accepting two parameters. round() 函数通过接受两个参数来工作。 The first is the number you want to round.第一个是您要舍入的数字。 The second is the number of decimal places to round to.第二个是要四舍五入的小数位数。

If you round 8.8333333333339 to 2 decimals, the correct answer is 8.83, not 8.84.如果将 8.8333333333339 取整到两位小数,则正确答案是 8.83,而不是 8.84。 The reason you got 8.83000000001 is because 8.83 is a number that cannot be correctly reprecented in binary, and it gives you the closest one.您得到 8.83000000001 的原因是因为 8.83 是一个无法正确表示为二进制的数字,它为您提供了最接近的数字。 If you want to print it without all the zeros, do as VGE says:如果你想在没有所有零的情况下打印它,按照 VGE 说的做:

print "%.2f" % 8.833333333339   #(Replace number with the variable?)

The easiest way to do this is by using the below function, which is built in:最简单的方法是使用以下内置函数:

format()

For example:例如:

format(1.242563,".2f")

The output would be:输出将是:

1.24

Similarly:同样:

format(9.165654,".1f")

would give:会给:

9.2

Just for the record.只是为了记录。 You could do it this way:你可以这样做:

def roundno(no):
    return int(no//1 + ((no%1)/0.5)//1)

There, no need for includes/imports在那里,不需要包含/导入

If you want to round, 8.84 is the incorrect answer.如果你想四舍五入,8.84 是不正确的答案。 8.833333333333 rounded is 8.83 not 8.84. 8.833333333333 四舍五入是 8.83 而不是 8.84。 If you want to always round up, then you can use math.ceil.如果你想总是四舍五入,那么你可以使用 math.ceil。 Do both in a combination with string formatting, because rounding a float number itself doesn't make sense.将两者与字符串格式结合使用,因为舍入浮点数本身没有意义。

"%.2f" % (math.ceil(x * 100) / 100)

Here is my solution for the round up/down problem这是我对向上/向下舍入问题的解决方案

< .5  round down

> = .5  round up

import math

def _should_round_down(val: float):
    if val < 0:
        return ((val * -1) % 1) < 0.5
    return (val % 1) < 0.5

def _round(val: float, ndigits=0):
    if ndigits > 0:
        val *= 10 ** (ndigits - 1)
    is_positive = val > 0
    tmp_val = val
    if not is_positive:
        tmp_val *= -1
    rounded_value = math.floor(tmp_val) if _should_round_down(val) else math.ceil(tmp_val)
    if not is_positive:
        rounded_value *= -1
    if ndigits > 0:
        rounded_value /= 10 ** (ndigits - 1)
    return rounded_value

# test
# nr = 12.2548
# for digit in range(0, 4):
#     print("{} decimals : {} -> {}".format(digit, nr, _round(nr, digit)))

# output
# 0 decimals : 12.2548 -> 12
# 1 decimals : 12.2548 -> 12.0
# 2 decimals : 12.2548 -> 12.3
# 3 decimals : 12.2548 -> 12.25

Use the decimal module: http://docs.python.org/library/decimal.html使用decimal模块: http : //docs.python.org/library/decimal.html

ََََََ ََََََََ

I have this code:我有这个代码:

tax = (tax / 100) * price

and then this code:然后这个代码:

tax = round((tax / 100) * price, 2)

round worked for me回合对我有用

Here is a simple function to do this for you:这是为您执行此操作的简单函数:

def precision(num,x):
    return "{0:.xf}".format(round(num))

Here, num is the decimal number.这里,num 是十进制数。 x is the decimal up to where you want to round a floating number. x 是要舍入浮点数的小数。

The advantage over other implementation is that it can fill zeros at the right end of the decimal to make a deciaml number up to x decimal places.与其他实现相比的优点是它可以在小数点的右端填充零,使十进制数最多为 x 位小数。

Example 1:示例 1:

precision(10.2, 9)

will return会回来

10.200000000 (up to 9 decimal points) 10.200000000(最多9位小数)

Example 2:示例 2:

precision(10.2231, 2)

will return会回来

10.22 (up to two decimal points) 10.22(最多两位小数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM