• python实现四舍五入


    由于python的round进行四舍五入时是遇六进一而不是遇五进一,所以要从新编写一个算法
    具体代码如下
    #coding:utf-8

    class round(object):

    #返回浮点数类型的值
    def roundF(self, value, digit):
    result = str(value)
    if (float(value) < 0):
    result = result[1:]
    if (result != ''):
    indexDec = result.find('.')
    if (indexDec > 0):
    decimal = result[indexDec + 1:]
    decimalCount = len(decimal)
    if (decimalCount > digit):
    xiaoshu = result[indexDec + digit + 1] # 第digit+1位小数
    if (int(xiaoshu) > 4):
    result = str(float(value) * -1 + pow(10, digit * -1))
    # 存在进位的可能,小数点会移位
    indexDec = result.find('.')
    result = result[:indexDec + digit + 1]
    else:
    result = result[:indexDec + digit + 1]
    else:
    lens = digit - len(result[indexDec:]) + 1
    for i in range(lens):
    result += '0'
    result = float(result) * -1
    return result
    else:
    if (result != ''):
    indexDec = result.find('.')
    if (indexDec > 0):
    decimal = result[indexDec + 1:]
    decimalCount = len(decimal)
    if (decimalCount > digit):
    xiaoshu = result[indexDec + digit + 1] # 第digit+1位小数
    if (int(xiaoshu) > 4):
    result = str(float(value) + pow(10, digit * -1))
    # 存在进位的可能,小数点会移位
    indexDec = result.find('.')
    result = result[:indexDec + digit + 1]
    else:
    result = result[:indexDec + digit + 1]
    else:
    lens = digit - len(result[indexDec:]) + 1
    for i in range(lens):
    result += '0'
    return float(result)

    #返回字符串类型的值
    def roundStr(self,value, digit):
    result = str(value)
    if (float(value) < 0):
    result = result[1:]
    if (result != ''):
    indexDec = result.find('.')
    if (indexDec > 0):
    decimal = result[indexDec + 1:]
    decimalCount = len(decimal)
    if (decimalCount > digit):
    xiaoshu = result[indexDec + digit + 1] # 第digit+1位小数
    if (int(xiaoshu) > 4):
    result = str(float(value) * -1 + pow(10, digit * -1))
    # 存在进位的可能,小数点会移位
    indexDec = result.find('.')
    result = result[:indexDec + digit + 1]
    else:
    result = result[:indexDec + digit + 1]
    else:
    lens = digit - len(result[indexDec:]) + 1
    for i in range(lens):
    result += '0'
    # result = float(result) * -1
    return '-'+result
    else:
    if (result != ''):
    indexDec = result.find('.')
    if (indexDec > 0):
    decimal = result[indexDec + 1:]
    decimalCount = len(decimal)
    if (decimalCount > digit):
    xiaoshu = result[indexDec + digit + 1] # 第digit+1位小数
    if (int(xiaoshu) > 4):
    dg=pow(10, digit * -1)
    result = str(float(value) +dg )
    # 存在进位的可能,小数点会移位
    indexDec = result.find('.')
    result = result[:indexDec + digit + 1]
    else:
    result = result[:indexDec + digit + 1]
    else:
    lens = digit - len(result[indexDec:]) + 1
    for i in range(lens):
    result += '0'
    return result

    if __name__ == '__main__':
    d=round().roundStr(2346.23502,4)
    print(d)
  • 相关阅读:
    ubuntu18.04安装dash-to-dock出错的问题
    使用SVN+Axure RP 8.0创建团队项目
    软件工程实践专题第一次作业
    C#单问号(?)与双问号(??)
    词根 ten 展开 持有 /tin/tent/tain “to hold”
    vscode 对js文件不格式化的修正方案 settings.json
    open cv java 可以 对图片进行分析,得到数据。考试答题卡 2B铅笔涂黑嘎达 识别
    bounties 赏金 bon = good 来自法语 bonjour 早上好
    class cl表示 汇聚 集合 ss表示 阴性 这里表示抽象
    git svn 提交代码日志填写规范 BUG NEW DEL CHG TRP gitz 日志z
  • 原文地址:https://www.cnblogs.com/shuyichao/p/10384332.html
Copyright © 2020-2023  润新知