zoukankan      html  css  js  c++  java
  • 【JAVA】Math.Round()函数常见问题“四舍5入”


    java.lang.Math.Round()使用时候,处理方式整理,方便以后查找
     
    /**
     * 测试函数 2014-01-10
     */
    public class TestMath {
        public static void main(String[] args) {
            System.out.println("小数点后第一位=5");
            System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));
            System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));
            System.out.println();
            System.out.println("小数点后第一位<5");
            System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));
            System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));
            System.out.println();
            System.out.println("小数点后第一位>5");
            System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));
            System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));
        }
    }
     
    小数点后第一位=5
    正数:Math.round(11.5)=12
    负数:Math.round(-11.5)=-11
    小数点后第一位<5
    正数:Math.round(11.46)=11
    负数:Math.round(-11.46)=-11
    小数点后第一位>5
    正数:Math.round(11.68)=12
    负数:Math.round(-11.68)=-12
     
    根据上面例子的运行结果,我们还可以按照如下方式总结,或许更加容易记忆:
    1、参数的小数点后第一位<5,运算结果为参数整数部分。
    2、参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。
    3、参数的小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。
    总结:大于五全部加,等于五正数加,小于五全不加。
     
     
     
    不及格的飞鱼 http://www.cnblogs.com/liuyongcn/
  • 相关阅读:
    [Python]计算豆瓣电影TOP250的平均得分
    [Golang]使用自建代理访问指定网站
    HDU 2689.Sort it-冒泡排序
    HDU 1728.逃离迷宫-BFS
    hihoCoder #1498.Diligent Robots
    POJ 2503.Babelfish-sscanf()函数+strcmp()函数+二分
    Codeforces 608 B. Hamming Distance Sum-前缀和
    Codeforces 608 A. Saitama Destroys Hotel
    sscanf()函数
    UVA 11461.Square Numbers
  • 原文地址:https://www.cnblogs.com/liuyongcn/p/3553298.html
Copyright © 2011-2022 走看看