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))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM