Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应,例如:ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.6)的结果为-11;floor的英文是地板,该方法就表示向下取整,Math.floor(11.6)的结果是11,Math.floor(-11.4)的结果-12;最难掌握的是round方法,他表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果是12,Math.round(-11.5)的结果为-11.Math.round( )符合这样的规律:小数点后大于5全部加,等于5正数加,小于5全不加。
让我们看看JDK的说明:  
(1)public static long round(double a)  
returns the closest long to the argument. the result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. in other words, the result is equal to the value of the expression:    
    
  (long)math.floor(a  +  0.5d)

(2)public static double floor(double a)  
  returns the largest(closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.special cases:
  if the argument value is already equal to a mathematical integer, then the result is the same as the argument.    
  if the argument is nan or an infinity or positive zero or negative zero, then the result is the same as the argument.  
    
  parameters:  
  a - a value.    
  returns:  
  the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer.

//import java.math.*;

public class RoundTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

// Math.round():Java中的四舍五入函数

System.out.println("Case1:小数点后第一位 = 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("Case2:小数点后第一位 < 5");

System.out.println("正数:Math.round(11.49) = " + Math.round(11.49));

System.out.println("负数:Math.round(-11.49) = " + Math.round(-11.49));

System.out.println("Case3:小数点后第一位 > 5");

System.out.println("正数:Math.round(11.69) = " + Math.round(11.69));

System.out.println("负数:Math.round(-11.69) = " + Math.round(-11.69));

System.out.println("结论:正数小数点后大于5则进位;负数小数点后小于以及等于5都舍去,大于5的则进位");

System.out.println("也就是说:小数点后大于5全部加,等于5正数加,小于5全不加");

}

}

Parameters
d the value to be rounded.
Returns
  • the closest integer to the argument.

Java Math.round()函数小结的更多相关文章

  1. Java中Math.round()函数

    Math.round(11.5) = 12; Math.round(-11.5) = -11; Math.round()函数是求某个数的整数部分,且四舍五入.

  2. Math.Round函数详解

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  3. Math.Round函数四舍五入

    Math.Round函数四舍五入的问题   今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...

  4. 一场Math.Round函数的误解

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  5. 【JAVA】Math.Round()函数常见问题“四舍5入”

    java.lang.Math.Round()使用时候,处理方式整理,方便以后查找   /**  * 测试函数 2014-01-10  */ public class TestMath {     pu ...

  6. Math.round() 函数返回一个数字四舍五入后最接近的整数。

    语法: Math.round(x); 参数:x 返回值:给定数字的值四舍五入到最接近的整数 描述: 如果参数的小数部分大于 0.5,则舍入到相邻的绝对值更大的整数. 如果参数的小数部分小于 0.5,则 ...

  7. Math.Round四舍五入

    Math.Round函数四舍五入的问题   今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...

  8. .Net中Math.Round与四舍五入

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  9. Math.floor,Math.ceil,Math.rint,Math.round用法

    一.Math.floor函数讲解 floor原意:地板.Math.floor函数是求一个浮点数的地板,就是求一个最接近它的整数,它的值小于或等于这个浮点数.看下面的例子: package com.qi ...

随机推荐

  1. 珠心算测验(0)<P2014_1>

    珠心算测验 (count.cpp/c/pas) 问题描述]  珠心算是一种通过在脑中模拟算盘变化来完成快速运算的一种计算技术.珠心算训练,既能够开发智力,又能够为日常生活带来很多便利,因而在很多学校得 ...

  2. PAT T1019 Separate the Animals

    暴力搜索加剪枝,二进制保存状态,set去重~ #include<bits/stdc++.h> using namespace std; ; string s[maxn]; struct n ...

  3. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  4. 【剑指Offer面试编程题】题目1355:扑克牌顺子--九度OJ

    题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...

  5. IP地址规划

    IP地址(Internet Protocol Address),缩写为IP Adress,是一种在Internet上的给主机统一编址的地址格式,也称为网络协议(IP协议)地址.它为互联网上的每一个网络 ...

  6. Set和Map集合的比较

     HashSet:数据进行hashCode比较,然后进行equals方法比较,根据比较结果进行排序.如果要对对象进行排序,对象类要重写hashCode和equals方法.TreeSet:如果要对对象进 ...

  7. nacos作为配置中心兼容xml配置文件

    最近公司想要用配置中心,因为公司用的有传统的spring项目,有springboot项目,为了兼容都能够采用配置中心,做了一些尝试,经过比较还是倾向于使用nacos,传统dubbo采用spring方式 ...

  8. kubernetes 使用flannel网络模式 错误分析

    今天按照网上和书上的要求,将目前的kubernetes网络换成flannel.其实配置起来还是很简单的,但是一旦出现了问题,将很难解决. 配置方法我这边不给出了.因为网上这样的教程一大把,在说下去也无 ...

  9. RF之目录结构、执行参数、用例标签 -6

    自动化项目的目录结构:        建议的目录结构... 以robot --pythonpath .  tc命令执行tc用例下面所有的用例 builtin库里面的Run Keywords方法实现初始 ...

  10. Kubernetes 的一些重要概念

    Cluster Cluseter 是计算.存储和网络资源的集合,Kubernetes 利用这些资源运行各种基于容器的应用. Master Master 是 Cluster 的大脑, 它的主要责任是调度 ...