round()Python似乎并不是四舍五入正常
-
09-06-2019 - |
解决方案
我不能帮助它的方式储存,但至少格式作品的正确:
'%.1f' % round(n, 1) # Gives you '5.6'
其他提示
格式能够正常工作,即使没有具有要轮:
"%.1f" % n
如果您使用的十进制的模块,你可以近似无需使用的'round'功能。这里是什么我用了四舍五入,尤其是当编写货币的应用:
Decimal(str(16.2)).quantize(Decimal('.01'), rounding=ROUND_UP)
这将返回的一个小数字,这是16.20.
round(5.59, 1)
工作正常。问题是,5.6不能完全代表在二浮点。
>>> 5.6
5.5999999999999996
>>>
作为Vinko说,可以使用的字符串的格式要做四舍五入为显示。
蟒蛇有一个 模块进行十进制算法 如果你需要的。
你得到5.6'如果你这样做 str(round(n, 1))
而不是只是 round(n, 1)
.
你可以开关的数据类型为一个整数:
>>> n = 5.59
>>> int(n * 10) / 10.0
5.5
>>> int(n * 10 + 0.5)
56
然后显示的数字插入该区域的十进制分离器。
但是, 吉米回答 是更好的。
浮点运算是受到轻微的,但是讨厌的、精确度的不准确之处。如果你可以的工作与整数或固定点,你将得到保证的精确度。
看看 小数点模块
小数"是基于一个浮点 模型,该模型的设计人 记住,一定有 最重要的指导原则 计算机,必须提供一个算术 这一工作在相同的方式 算术,人们学习 学校。" –摘自小数 算术规格。
和
数字可以代表的 准确。与此相反,数字样1.1 2.2没有一个确切的 陈述二浮 点。终端用户通常不会 预计1.1+2.2显示 3.3000000000000003因为它与二元浮点。
十进制提供的种行动,使它容易编写的应用程序,需要浮点运算 还 需要明这些结果在人类可读格式,例如会计。
这是一个很大的问题。尝试一下这个代号:
print "%.2f" % (round((2*4.4+3*5.6+3*4.4)/8,2),)
它显示4.85.然后你做的事:
print "Media = %.1f" % (round((2*4.4+3*5.6+3*4.4)/8,1),)
它显示了4.8.你的计算方面的确切答案是4.85,但是如果你尝试:
print "Media = %.20f" % (round((2*4.4+3*5.6+3*4.4)/8,20),)
你可以看到的真理:浮点存储最近的有限的总和分,其特点是权力的两个。
你可以使用的字符串格式操作员 %
, 类似sprintf.
mystring = "%.2f" % 5.5999
printf 在吮吸者。
print '%.1f' % 5.59 # returns 5.6
完美的作品
format(5.59, '.1f') # to display
float(format(5.59, '.1f')) #to round
我这样做:
int(round( x , 0))
在这种情况下,我们第一轮正在单元的水平,然后我们转换到整数,以避免打印一个浮动。
所以
>>> int(round(5.59,0))
6
我认为这个答案,比格式化,这也使得更多的桑给我使用的轮功能。
代码:
x1 = 5.63
x2 = 5.65
print(float('%.2f' % round(x1,1))) # gives you '5.6'
print(float('%.2f' % round(x2,1))) # gives you '5.7'
输出:
5.6
5.7
这里就是我看到圆失败。什么如果你想圆2这些数字的确到小数点后一位数字的地方吗?23.45 23.55 我的教育是从四舍五入这些你应该得到:23.4 23.6 "规则"被认为你应该圆如果前一人数是奇数,不是圆的,如果上述数字,甚至.轮功能在蟒蛇只截断的5个。
问题是只有当最后一位数字是5。例如。0.045内部存储为0.044999999999999...你可以简单地增加最后一位数字为6和圆关闭。这会给你想要的结果。
import re
def custom_round(num, precision=0):
# Get the type of given number
type_num = type(num)
# If the given type is not a valid number type, raise TypeError
if type_num not in [int, float, Decimal]:
raise TypeError("type {} doesn't define __round__ method".format(type_num.__name__))
# If passed number is int, there is no rounding off.
if type_num == int:
return num
# Convert number to string.
str_num = str(num).lower()
# We will remove negative context from the number and add it back in the end
negative_number = False
if num < 0:
negative_number = True
str_num = str_num[1:]
# If number is in format 1e-12 or 2e+13, we have to convert it to
# to a string in standard decimal notation.
if 'e-' in str_num:
# For 1.23e-7, e_power = 7
e_power = int(re.findall('e-[0-9]+', str_num)[0][2:])
# For 1.23e-7, number = 123
number = ''.join(str_num.split('e-')[0].split('.'))
zeros = ''
# Number of zeros = e_power - 1 = 6
for i in range(e_power - 1):
zeros = zeros + '0'
# Scientific notation 1.23e-7 in regular decimal = 0.000000123
str_num = '0.' + zeros + number
if 'e+' in str_num:
# For 1.23e+7, e_power = 7
e_power = int(re.findall('e\+[0-9]+', str_num)[0][2:])
# For 1.23e+7, number_characteristic = 1
# characteristic is number left of decimal point.
number_characteristic = str_num.split('e+')[0].split('.')[0]
# For 1.23e+7, number_mantissa = 23
# mantissa is number right of decimal point.
number_mantissa = str_num.split('e+')[0].split('.')[1]
# For 1.23e+7, number = 123
number = number_characteristic + number_mantissa
zeros = ''
# Eg: for this condition = 1.23e+7
if e_power >= len(number_mantissa):
# Number of zeros = e_power - mantissa length = 5
for i in range(e_power - len(number_mantissa)):
zeros = zeros + '0'
# Scientific notation 1.23e+7 in regular decimal = 12300000.0
str_num = number + zeros + '.0'
# Eg: for this condition = 1.23e+1
if e_power < len(number_mantissa):
# In this case, we only need to shift the decimal e_power digits to the right
# So we just copy the digits from mantissa to characteristic and then remove
# them from mantissa.
for i in range(e_power):
number_characteristic = number_characteristic + number_mantissa[i]
number_mantissa = number_mantissa[i:]
# Scientific notation 1.23e+1 in regular decimal = 12.3
str_num = number_characteristic + '.' + number_mantissa
# characteristic is number left of decimal point.
characteristic_part = str_num.split('.')[0]
# mantissa is number right of decimal point.
mantissa_part = str_num.split('.')[1]
# If number is supposed to be rounded to whole number,
# check first decimal digit. If more than 5, return
# characteristic + 1 else return characteristic
if precision == 0:
if mantissa_part and int(mantissa_part[0]) >= 5:
return type_num(int(characteristic_part) + 1)
return type_num(characteristic_part)
# Get the precision of the given number.
num_precision = len(mantissa_part)
# Rounding off is done only if number precision is
# greater than requested precision
if num_precision <= precision:
return num
# Replace the last '5' with 6 so that rounding off returns desired results
if str_num[-1] == '5':
str_num = re.sub('5$', '6', str_num)
result = round(type_num(str_num), precision)
# If the number was negative, add negative context back
if negative_number:
result = result * -1
return result
什么约:
round(n,1)+epsilon