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. ThinkPHP5 动态生成图片缩略图

    需求场景 不同终端(PC端.手机端.平板),不同界面(列表页.详情页),对图片大小的要求不一样, 如果所有场景下都使用同一尺寸的图片,势必对会网络带宽及服务器性能造成一定的影响,由此需要服务器端能够根 ...

  2. Mybatis之foreach用法----List、Array、Map三种类型遍历

    在mybatis的xml文件中构建动态sql语句时,经常会用到标签遍历查询条件.特此记录下不同情况下书写方式!-------仅供大家参考------ 1. foreach元素的属性 collectio ...

  3. 通过getGeneratedKeys获取记录的主键

    Connection con=null; PreparedStatement ps=null; ResultSet rs=null; try { //建立连接 con= JDBCUtils.getCo ...

  4. java 连接远程Linux 服务器

    创建闭锁,确保能连接到zk服务器. // 创建闭锁final CountDownLatch countDownLatch = new CountDownLatch(1); String connect ...

  5. Python字符乱码

    content = b'{"log_id": 5507183146687669657, "words_result_num": 2, "words_r ...

  6. vue导航守卫和axios拦截器的区别

    在Vue项目中,有两种用户登录状态判断并处理的情况,分别为:导航守卫和axios拦截器. 一.什么是导航守卫? vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.(在路由跳转时 ...

  7. 学习进度-11 RDD 编程初级实践

    一. 请到本教程官网的“下载专区”的“数据集”中下载 chapter5-data1.txt,该数据集包含 了某大学计算机系的成绩,数据格式如下所示: Tom,DataBase,80 Tom,Algor ...

  8. lvextend 扩容后, df -h 看到的却还是原来的大小

    [root@stb ~]# df -hFilesystem                  Size  Used Avail Use% Mounted on/dev/mapper/vg_stb-lv ...

  9. .net设置浏览器缓存和跨域的几种方法

    .自定义过滤器属性 public class NoCacheAttribute : FilterAttribute, IActionFilter { public void OnActionExecu ...

  10. Android 如何从系统图库中选择图片

    转:http://blog.csdn.net/tody_guo/article/details/7560270 这几天我都在做Android的App,同时学习它的API,我将分享一些我学到的东西,比如 ...