zoukankan      html  css  js  c++  java
  • 关于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():正常的标准舍入,就像我们学的数学课本上面的规则
    
    
    
    
    
  • 相关阅读:
    鼠标移上,内容显示
    Jquery横向菜单和纵向菜单的收起与展开
    适配不同大小浏览器——固定排班
    jQuery UI Widgets-menu
    Web前端的35个jQuery小技巧-转载
    android中listview中包含ratingbar响应不了点击事件
    点击空白区域,键盘向下收缩
    时间轮 Dialog 最简单的时间轮
    android 获取电话本中的联系人列表
    《网红经济》读后感
  • 原文地址:https://www.cnblogs.com/one-tom/p/10146127.html
Copyright © 2011-2022 走看看