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.

转载于:https://www.cnblogs.com/leihupqrst/p/3666256.html

Java Math.round()函数小结相关推荐

  1. java的round函数怎么用_Java Math round()用法及代码示例

    java.lang.Math.round()是内置数学函数,它返回最接近参数的long.通过将1/2相加,将结果四舍五入为整数,再加上1/2后取结果的下限,并将结果强制转换为long类型. 如果参数为 ...

  2. java的round函数加点差_【JAVA】Math.Round()函数常见问题“四舍5入”

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

  3. java Math.round()比较特殊的四舍五入

    java Math.round()比较特殊的四舍五入 public class MathTest {        public static void main(String[] args) {  ...

  4. java math round小数_Java Math round() 使用方法及示例

    Java Math round() 使用方法及示例 Java Math round()方法将指定的值四舍五入为最接近的int或long值,然后将其返回. 也就是说,1.2四舍五入为1,1.8四舍五入为 ...

  5. 详解Math.round函数

    1.代码如下, public class TestMathRound {public static void main(String[] args) {System.out.println(" ...

  6. java math round小数_Java——Math的round方法

    代码如下,后面的注释是输出的结果 public static voidmain(String[] args) { System.out.println(Math.round(0.399));//0 S ...

  7. Math.Round函数四舍五入的问题

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

  8. Math.Round函数

    Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应,例如:ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11. ...

  9. Java - Math.round(1.5) 等于多少?Math.round(-1.5) 等于多少?

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程 Math.round(1.5)的返回值是2,Math.round(-1.5)的返回值是-1 ...

最新文章

  1. 32.Docker安装MongoDb
  2. 【助教】关于代码提交相关注意事项
  3. 机器学习之PCA原理
  4. 获2017中国最佳创业投资机构百强,西高投二次创业实现超越
  5. 程序解析excel中的图片_Excel表格中链接图片操作方法,以后查看图片点点鼠标就可以了...
  6. 关于数据库求候选键问题
  7. 固定时间收敛的控制器设计(基础知识)
  8. python oct_安装在python oct2py中使用的gnuoctave
  9. linux内核mproject函数,把linux驱动独立于内核外编译--示例
  10. recovery_minui解说
  11. vue控制滚动条滑到某个位置
  12. iconv-lite
  13. Mentor Expedition如何极坐标旋转添加器件?
  14. 不写DAX实现TopN和其他
  15. python中getattr()和setattr()的使用
  16. Linux操作系统实践期中考总结(选择题)
  17. 2021-2022学年——嵌入式系统实践实验5_Time实验
  18. 【大数据之Hadoop3.x】
  19. 飞腾CPU体系结构之虚拟地址
  20. 虚幻4 附加 组合 图层

热门文章

  1. 使用谷歌语音识别打造语音管家HiVoice
  2. 创建你的第一个游戏Pong——让我们编写Pong
  3. LeetCode(506)——相对名次(JavaScript)
  4. 今天来总结一下CSS中有哪些定位
  5. php arff文件,python实现txt文件格式转换为arff格式
  6. iTunes是什么意思
  7. 都是打工的,为啥职场中存在着那么多勾引斗角?
  8. “有钱人大多不快乐”这种观点,只是普通人的心理安慰吗?
  9. 你还记得珠算的口诀吗?
  10. 为什么我只写微头条,粉丝一天就增加700多人?