我是Python的新手。我正在获取传感器读数,小数位数太多(x.xxxxxxxx
)
我想把读数限制在4位小数(x.xxxx
)。你知道吗
from altimu10v5.lsm6ds33 import LSM6DS33
from altimu10v5.lis3mdl import LIS3MDL
from altimu10v5.lps25h import LPS25H
from time import sleep
lsm6ds33 = LSM6DS33()
lsm6ds33.enable()
lps25h = LPS25H()
lps25h.enable()
lis3mdl = LIS3MDL()
lis3mdl.enable()
while True:
#print(lsm6ds33.get_accelerometer_raw())
print("{:.2f}".format.lsm6ds33.get_accelerometer_g_forces())
sleep(0.4)
运行此代码时,会收到以下错误消息:
" attribute error : ' builtin_function_or_method' object has no attribute 'lsm6ds33'
尝试:
编辑:如果您有一个要编辑的值数组string.format格式(),使用for循环迭代每个值,并一次格式化一个值。像这样:
给定数组[-0.5555,-0.78788,0.878888],上述循环将输出:
相关问题 更多 >
编程相关推荐