round()函数的文档声明您传递一个数字,并将小数点后的位置传递给round.因此它应该这样做:
n = 5.59
round(n, 1) # 5.6
Run Code Online (Sandbox Code Playgroud)
但是,实际上,良好的旧浮点怪异爬进来,你会得到:
5.5999999999999996
Run Code Online (Sandbox Code Playgroud)
出于UI的目的,我需要显示5.6.我在互联网上搜索并发现一些文档,这取决于我的Python实现.不幸的是,这发生在我的Windows开发机器和我尝试过的每台Linux服务器上.在这里也看到.
没有创建我自己的圆形库,有什么方法可以解决这个问题吗?
我想从浮点数中删除数字,使点后面有一个固定的位数,如:
1.923328437452 -> 1.923
Run Code Online (Sandbox Code Playgroud)
我需要将字符串输出到另一个函数,而不是打印.
此外,我想忽略丢失的数字,而不是围绕它们.
我试图在Python中截断一个十进制值.我不想舍入它,而只是显示小数值达到指定的精度.我尝试了以下方法:
d = 0.989434
'{:.{prec}f}'.format(d, prec=2)
Run Code Online (Sandbox Code Playgroud)
这使其达到0.99.但我实际上希望输出为0.98.显然,round()不是一种选择.有没有办法做到这一点?或者我应该回到代码并将所有内容更改为decimal?
谢谢.
我需要向下舍入,它应该是两位小数.
试过以下,
a = 28.266
print round(a, 2)
Run Code Online (Sandbox Code Playgroud)
28.27
但预期值仅为28.26.
我在两个靠近的数据框中有经度和纬度。如果我运行精确的相似性检查,例如
test_similar = test1_latlon.loc[~test1_latlon['cr'].isin(test2_latlon['cr'])]
Run Code Online (Sandbox Code Playgroud)
我遇到了很多失败,因为很多数字都偏离了小数点后第五位。我想截断小数点后第三位。我见过人们格式化,因此它显示被截断,但我想更改实际值。使用round()四舍五入数据,我会得到更多错误,那么有没有办法只删除小数点后 3 位呢?
Currently I am trying to solve a problem, where I am supposed to print the answer upto two decimal points without rounding off. I have used the below code for this purpose
import math
a=1.175 #value of a after some division
print(math.floor(a*100)/100)
Run Code Online (Sandbox Code Playgroud)
The output we get is:
1.17 #Notice value which has two decimal points & not rounded
Run Code Online (Sandbox Code Playgroud)
但当我尝试打印一个可整除的数字时,真正的问题就开始了,小数点后只显示一个零。我使用了与上面相同的代码,但现在
a=25/5 #Now a is perfectly divisible
print(math.floor(a*100)/100)
Run Code Online (Sandbox Code Playgroud)
现在显示的输出是
5.0 #Notice only one decimal place is printed
Run Code Online (Sandbox Code Playgroud)
必须采取什么措施来纠正这个错误?
python ×6
decimal ×2
rounding ×2
truncate ×2
dataframe ×1
fixed-point ×1
formatting ×1
pandas ×1