推广型网站建设模板,大连建设网站制作,做网站图片广告推广怎么忽悠人的,肥西县建设局网站Python decimal 模块Python中的浮点数默认精度是15位。Decimal对象可以表示任意精度的浮点数。getcontext函数用于获取当前的context环境#xff0c;可以设置精度、舍入模式等参数。#在context中设置小数的精度
decimal.getcontext().prec 100通过字符串初始化Decimal类型的变…Python decimal 模块Python中的浮点数默认精度是15位。Decimal对象可以表示任意精度的浮点数。getcontext函数用于获取当前的context环境可以设置精度、舍入模式等参数。#在context中设置小数的精度
decimal.getcontext().prec 100通过字符串初始化Decimal类型的变量因为通过浮点数初始化Decimal类型的变量会导致精度的丢失# 浮点数的初始化
a decimal.Decimal(3.14159265)setcontext函数decimal.ROUND_HALF_UP 对浮点数四舍五入import decimal
x decimal.Decimal(1.23456789)
context decimal.Context(prec4,roundingdecimal.ROUND_HALF_UP)
decimal.setcontext(context)
y1 x
y2 x*2
print(y1,y1)
print(y2,y2)y1 1.23456789
y2 2.469localcontext函数用于创建一个新的context环境可以在该环境中设置精度、舍入模式等参数不会影响全局的context环境。import decimal
x decimal.Decimal(1.23456789)
context0 decimal.Context(prec9,roundingdecimal.ROUND_HALF_UP)
decimal.setcontext(context0)
y1 x * 2
print(y1,y1)with decimal.localcontext() as context:context.prec 4context.rounding decimal.ROUND_HALF_UPy2 x * 2print(y2,y2)y1 2.46913578
y2 2.469