关于python保留几位小数,不进行四舍五入的方法

法一

def cut(num, c):
    str_num = str(num)
    return float(str_num[:str_num.index('.') + 1 + c])

print (cut(2.999,2))
# 2.99
print (cut(1.00000001,8))
# 1.00000001

法二

def cut(num, c):
    c=10**(-c)
    return (num//c)*c

print (cut(2.999,2))
# 2.99
print (cut(1.00000001,8))
# 1.0
posted @ 2016-12-20 15:32  twfb  阅读(8786)  评论(0编辑  收藏  举报