floor 返回不大于的最大整数 
round 则是4舍5入的计算,入的时候是到大于它的整数
round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。

ceil 则是不小于他的最小整数

看例子

  Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

测试程序如下:

  1. public class MyTest {
  2. public static void main(String[] args) {
  3. double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
  4. for (double num : nums) {
  5. test(num);
  6. }
  7. }
  8. private static void test(double num) {
  9. System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
  10. System.out.println("Math.round(" + num + ")=" + Math.round(num));
  11. System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
  12. }
  13. }

运行结果 
Math.floor(1.4)=1.0 
Math.round(1.4)=1 
Math.ceil(1.4)=2.0 
Math.floor(1.5)=1.0 
Math.round(1.5)=2 
Math.ceil(1.5)=2.0 
Math.floor(1.6)=1.0 
Math.round(1.6)=2 
Math.ceil(1.6)=2.0 
Math.floor(-1.4)=-2.0 
Math.round(-1.4)=-1 
Math.ceil(-1.4)=-1.0 
Math.floor(-1.5)=-2.0 
Math.round(-1.5)=-1 
Math.ceil(-1.5)=-1.0 
Math.floor(-1.6)=-2.0 
Math.round(-1.6)=-2 
Math.ceil(-1.6)=-1.0

http://blog.csdn.net/foart/article/details/4295645

1.利用Math.round()的方法:

两个int型的数相除,结果保留小数点后两位:

int a=1188;
int b=93;
double c;

c=(double)(Math.round(a/b)/100.0);//这样为保持2位
打印结果:c=0.12

c=new Double(Math.round(a/b)/1000.0);//这样为保持3位
打印结果:c=0.012

2.另一种办法
import java.text.DecimalFormat;

DecimalFormat df2  = new DecimalFormat("###.00");//这样为保持2位

DecimalFormat df2  = new DecimalFormat("###.000");//这样为保持3位

System.out.println(df2.format(double类型的变量));

PS:

Math.round()的作用:

double a=123.55
System.out.println(Math.round(a));
打印结果:124

http://blog.csdn.net/evatian/article/details/4398016

JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结的更多相关文章

  1. Android java处理保留小数点后几位

    方式一: 四舍五入  double   f   =   111231.5585;  BigDecimal   b   =   new   BigDecimal(f);  double   f1   = ...

  2. JAVA除法保留小数点后两位的两种方法

      1.(double) (Math.round(sd3*10000)/10000.0);  这样为保持4位 (double) (Math.round(sd3*100)/100.0); 这样为保持2位 ...

  3. Java中保留小数点后几位

    不想多说啥了..ε=(´ο`*)))唉  基础都给忘了..今天比赛 跌入十八层地狱.... 用DecimalFormat对象的format方法进行格式化.. package cn.test; impo ...

  4. php number_format()保留小数点后几位

    [PHP_保留两位小数的相关函数] php保留两位小数并且四舍五入 Php代码   1     $num = 123213.666666;  2     echo sprintf("%.2f ...

  5. js保留小数点后N位的方法介绍

    js保留小数点后N位的方法介绍 利用toFixed函数 代码如下 复制代码 <script language="javascript"> document.write( ...

  6. php number_format()保留小数点后几位有效数的函数 千位分组来格式化数字(转)

    PHP保留小数点后2位的函数number_format number_format(带小数点的书,小数点后保留的位数) number_format(8.3486,2);  //取得小数点后2位有效数/ ...

  7. C#保留小数点后几位

    String.Format("{0:N1}", a) 保留小数点后一位 String.Format("{0:N2}", a) 保留小数点后两位 String.F ...

  8. 实现js保留小数点后N位的代码

    在JS中,一般实现保留小数点后N位的话,都是利用toFixed函数 <script language="javascript"> document.write(&quo ...

  9. 格式化 float 类型,保留小数点后1位

    """  练习 :   小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,   并用字符串格式化显示出'xx.x%',只保留小数点后1位: &qu ...

随机推荐

  1. 关于我和Github不得不说的一些小事

    你好,我叫黄雅婷,学号是1413042031,网络工程142班.因为小时候家里有很多课外书,有关神话和科学方面的杂志和书籍等,所以从小就喜欢看书,现在比较不挑,什么书都喜欢看,就是给我本字典,我也能看 ...

  2. iOS:Size Classes的使用

    iOS 8在应用界面的可视化设计上添加了一个新的特性-Size Classes,对于任何设备来说,界面的宽度和高度都只分为两种描述:正常和紧凑.这样开发者便可以无视设备具体的尺寸,而是对这两类和它们的 ...

  3. Selenium Waits

    Selenium高级功能包含查找等待, Selenium的查找等待有两种方式, 隐式等待(Implicit Waits)和显示等待(Explicit Waits): 这里写下我对两者的理解, 1. 隐 ...

  4. magento首页调用最新产品

    这个需要我们自己添加一个block块供我们调用,可参考new products的block类,建立文件app/code/core/Mage/Catalog/Block/Product/Special. ...

  5. iOS-appDelegate 生命周期

    - (void)applicationWillResignActive:(UIApplication *)application 说明:当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事 ...

  6. opencv3.1包安装

    由于之前零零碎碎安装了很多必要的库: 现在只需: (1)下载和解压包 https://github.com/daveselinger/opencv/tree/3.1.0-with-cuda8 这里的分 ...

  7. CentOS 6.6 FTP install

    /************************************************************************* * CentOS 6.6 FTP install ...

  8. windows 任务栏图标宽度固定

    这个需要修改注册表. win+r regedit ->enter 找到以下项 HKEY_CURRENT_USER-Control Panel-Desktop-WindowsMetrics 新建字 ...

  9. SQL编写

    //用户表,用户ID,用户名称create table t_user (user_id int,username varchar(20));//用户帐户表,用户ID,用户余额(单位分)create t ...

  10. 4-3 yum命令

    1.常用yum命令 <1>查询 yum list #查询所有可用软件包列表(以 包名 - 版本 - yum源所在名称 格式显示) yum search 关键字 #搜索服务器上所有和关键字相 ...