精度
精度
加减精度值
from decimal import Decimal
JD = Decimal(0.000001)
if oml_row['optionmarginwarningratio'] == None:
eventtitle2 = '【' + o32_row['accountid'] + '】项目下【' + o32_row['assetno'] + \
'】资产单元的保证金占用比率超出【警示】阈值 { 保证金比率:' + \
str(round(usedmarginratio_ * 100, 2)) + \
'%,限制值:' + str(round(oml_row['optionmarginerrorratio'] * 100, 2)) + '%}'
# 只有二:警示
if (usedmarginratio_ - JD >= oml_row['optionmarginerrorratio']) or (usedmarginratio_ + JD >= oml_row['optionmarginerrorratio']):
python内置或str的保留两位小数
方法一:使用字符串格式化
a = 12.345
print("%.2f" % a)
# 12.35
方法二: 使用round内置函数
a = 12.345
a1 = round(a, 2)
print(a1)
# 12.35
方法三: 使用decimal模块
from decimal import Decimal
a = 12.345
Decimal(a).quantize(Decimal("0.00"))
Decimal('12.35')
仅保留两位小数,无需四舍五入
方法一: 使用序列中切片
a = 12.345
str(a).split('.')[0] + '.' + str(a).split('.')[1][:2]
'12.34'
方法二: 使用re模块
import re
a = 12.345
re.findall(r"\d{1,}?\.\d{2}", str(a))
['12.34']