(round
(1.5),
round
(2.5),
round
(3.5))
# 輸出為 2, 2, 4
dataframefrom decimal import decimal, round_half_up
defround_dec
(n, d=2)
: s =
'0.'
+'0'
* d return decimal(
str(n)
).quantize(decimal(s)
, rounding=round_half_up)
(round_dec(
3.545,2
))# 保留兩位小數
defmyround
(res)
:if res==
""or res==0:
returndata=
0.00
else:if
isinstance
(res,
float):
new_res=res*
100 returndata=
round
(new_res)
/100
elif
isinstance
(res,
str)
: new_res=
float
(res.replace(
',','')
)*100 returndata =
round
(new_res)
/100
else
: new_res=res*
100 returndata =
round
(new_res)
/100
return
str(returndata)
自己的解決思路:import pandas as pd
import numpy as np
import math
df = pd.dataframe(
)# 記錄真實值
df["真實值"
]= df[
"除數"
]/ df[
"被除數"
]# 記錄整數部分
df["輔助整數列"
]= df[
"真實值"].
(lambda x: math.modf(x)[1
])# 記錄小數部分,因為我的最後結果精度為只保留整數部分,所以我只需要保留乙個小數點位進行判斷是否需要進製操作
df["輔助小數列"
]= df[
"真實值"].
(lambda x:
str(math.modf(x)[0
]).split(
".")[1
][0]
)# 小數點後的第一位是為5,則向上取整,不是5則呼叫原np.round就行了
df["期望值修正"
]= df.
(lambda x: x.輔助整數列 +1if
(x.輔助小數列 ==
"5")
else np.
round
(x.真實值)
, axis=
1)
直接把數值加多一位的0.1後再使用round四捨五入,比如+0.01
df[
'test'
]= df[
'test'].
(lambda x: x +
0.01).
(round
)
關於四捨五入
四捨五入 轉http nssoft.showdoc.asp?did 267 delphi的四捨五入函式round,對於 5的情況,整數部分是奇數,那麼會round up,偶數會round down,例如 x round 17.5 x 18 x round 12.5 x 12 請使用下面的函式代替ro...
python中四捨五入精度問題
python存float的時候如財務資料等需要四捨五入兩位數,但是python自帶的四捨五入方法偶爾會存在 五舍六入 提供兩種處理函式 from decimal import decimal,round half up def round dec n,d 2 s 0.0 d return decim...
qt的四捨五入 Qt(C )四捨五入
qt現在是四捨六入五成雙,要想四捨五入得自己想辦法,實現如下 include include double c1 3.435 double c2 3.445 double c3 3.4351 double c4 3.4451 double c5 3.445 qdebug qdebug qdebug ...