Java Math.round()函数小结
| 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全不加。 | 
(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()函数小结的更多相关文章
- Java中Math.round()函数
		
Math.round(11.5) = 12; Math.round(-11.5) = -11; Math.round()函数是求某个数的整数部分,且四舍五入.
 - Math.Round函数详解
		
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
 - Math.Round函数四舍五入
		
Math.Round函数四舍五入的问题 今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...
 - 一场Math.Round函数的误解
		
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
 - 【JAVA】Math.Round()函数常见问题“四舍5入”
		
java.lang.Math.Round()使用时候,处理方式整理,方便以后查找 /** * 测试函数 2014-01-10 */ public class TestMath { pu ...
 - Math.round() 函数返回一个数字四舍五入后最接近的整数。
		
语法: Math.round(x); 参数:x 返回值:给定数字的值四舍五入到最接近的整数 描述: 如果参数的小数部分大于 0.5,则舍入到相邻的绝对值更大的整数. 如果参数的小数部分小于 0.5,则 ...
 - Math.Round四舍五入
		
Math.Round函数四舍五入的问题 今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...
 - .Net中Math.Round与四舍五入
		
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
 - Math.floor,Math.ceil,Math.rint,Math.round用法
		
一.Math.floor函数讲解 floor原意:地板.Math.floor函数是求一个浮点数的地板,就是求一个最接近它的整数,它的值小于或等于这个浮点数.看下面的例子: package com.qi ...
 
随机推荐
- Spring Boot 2 实战:如何自定义 Servlet Filter
			
1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter 来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等 ...
 - win10程序无法正常启动0xc0000142
			
office用的好好的,今天一早打开电脑,突然就打不开了.显示如图: 我个人猜测可能还是昨天更新其他软件的时候导致的,有个软件更新后,让我重启,当时因为忙,就没有重启.今天一开机,就发现office用 ...
 - Python爬取51job实例
			
用Python爬取51job里面python相关职业.工作地址和薪资. 51job上的信息 程序代码 from bs4 import BeautifulSoup from urllib.request ...
 - Vue入口页
			
Template里面的App就是在这个实例里面注册的App组件 也就是整个过程就是将el所标识的元素替换成<App/> 而App就是在此实例注册的App组件.
 - CSP-J2019 加工零件
			
Background: 之前 $noip $死了,泥萌都说 \(noip SPFA\) 了,现在 \(noip\) 复活了,所以 \(SPFA\) 也复活了. (注:这里的 \(noip\) 跟 \( ...
 - Python内置模块-logging
			
一.初识logging模块 import logging logging.debug("debug message") #级别最低,只有在诊断问题时才有兴趣的详细信息. loggi ...
 - 页面渲染时js阻塞的解决方法
			
一般地,一个包含外部样式表文件和外部脚本文件的HTML载入和渲染过程是这样的: 浏览器下载HTML文件并开始解析DOM. 遇到样式表文件link[rel=stylesheet]时,将其加入资源文件下载 ...
 - 1. 使用 Docker 安装 Kong
			
Create a Docker network $ docker network create kong-net Start Database $ docker run -d --name kong- ...
 - Java程序与其它进程的数据通信
			
Java程序中可以启动其他的应用程序,这种在Java中启动的进程称为子进程,启动子进程的Java程序称为父进程,其实这个父进程就是一个Java虚拟机1.在Java程序中可以用Process类的实例对象 ...
 - Lesson 10 Silicon valley
			
What does the computer industry thrive on apart from anarchy? Technology trends may push Silicon Val ...