• 关于python中的round()和javascript中的round()的比较


    ps:python的round()和javascript的round()的比较:
        javascript:Math.round():正常的标准舍入,就像我们学的数学课本上面的规则
    
    
    python:python中的round()会根据版本的不同而出现不同的结果。整体的是最靠近当前偶数的规则  
    01:当小数部分只为0.5时,整数部分如果为奇数则进1,如果为偶数则舍去小数部分
            print(round(3.5))   #4
            print(round(-3.5))   #-4
            print(round(4.5))   #4
            print(round(-4.5))   #-4
            
            02:小数部分不为0.5时,按标准的四舍五入
            print(round(3.6))   #4
            print(round(4.6))   #5
            print(round(4.4))   #4
            print(round(-4.4))  #-4
            
            03:保留小数部分时候
            a:当小数部分需要保留数位的下一位是5时,保留数位置为偶数则进1,奇数舍去。
            print(round(2.345,2))  #2.35
            print(round(-2.345,2))  #-2.35
            print(round(-2.335,2))  #-2.33
            b:当小数部分需要保留数位的下一位不是5时,按标准四舍五入(逢5进1)
            print(round(2.344,2))   #2.34
            print(round(2.347,2))  #2.35
            print(round(-2.347,2))  #-2.35
    ps:拓展知识点:Math.ceil(),Math.floor(),Math.round()的区别
    Math.ceil():小数后面的数字不管多少,直接进位,比如,12.1和12.5,12.6都直接返回13,ceil是向上舍入的(math.ceiling(天花板除法)。)
        Math.floor():不管小数点后的数字是多少,多大,都是直接舍去,不进位比如 12.1和12.5,12.9都是直接返回12,floor是向下舍入的。
        Math.round():正常的标准舍入,就像我们学的数学课本上面的规则
    
    
    
    
    
  • 相关阅读:
    信息收集-DNS
    Xshell下载
    JSP
    本地网络配置
    P1485 火枪打怪
    P4155 [SCOI2015]国旗计划
    P1017 [NOIP2000 提高组] 进制转换
    P1013 [NOIP1998 提高组] 进制位
    P1011 [NOIP1998 提高组] 车站
    CF841B Godsend
  • 原文地址:https://www.cnblogs.com/one-tom/p/10146127.html
Copyright © 2020-2023  润新知