精确四舍五入

1
2
3
4
5
6
7
8
9
import decimal
def round_up(x, y):
"""
四舍五入
:param x: 具体数值 type: str
:param y: 保留小数位数 type: str example: "0.01" 取整 “1.0”
:return: str
"""
return decimal.Decimal(x).quantize(decimal.Decimal(y), rounding=decimal.ROUND_UP)

时间戳转换当地时间

1
2
3
4
5
6
7
8
9
10
11
import time
import datetime
def timestamp_to_localtime(timestamp):
"""
时间戳转换当地时间
:param timestamp: 时间戳
:return: str
"""
time_local = time.localtime(timestamp)
dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
return dt