python保留三位小数向下取值

#取三位小时---format会四舍五入
b = 0.17777
print("{:.3%}".format(b))

#取三位小时---不四舍五入
# 方法一:
a =12.345666
b = str(a)#转换成字符串
c =b.split('.')#将整数和小数分割
e =c[0] #取列表第一个字符(整数)
f = c[1] #取列表第二个字符(小数)
g = f[0:2] #取第二个字符(小数)的前两位
h = float(e+'.'+g) #字符串拼接,并转换成浮点型
# print(type(b))
print(b)
print(c)
print(e)
print(f)
print(g)
print(type(h))
# 方法二:
# def cut(num, c):
# c=10**(-c)
# return (num//c)*c
#
# print (cut(2.998888888,6))
posted @   小正哥  阅读(1114)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示