Python保留几位小数,不进行四舍五入的方法
方法一:
def round_two(num, c): """ 保留2位小数,并向下取整 :param num: 需要操作的原始数据 :param c: 截取位数 :return: 2位小数,并向下取整 """ str_num = str(num) if '.' in str_num and (c + 1) <= len(str_num[str_num.index('.') + 1:]): return float(str_num[:str_num.index('.') + 1 + c]) else: return float('{:.2f}'.format(num))
方法二:
def cut(num, c): if c == 0: return int(num) else: return round(num / (10 ** c)) * (10 ** c)
本文来自博客园,作者:术科术,转载请注明原文链接:https://www.cnblogs.com/shukeshu/p/15055429.html