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. 【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  2. js中获取样式的俩种方法 style.color和style['color'] 区别

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 让IE9支持html5

    IE10以上才算是真正支持了html5 ,但仍然有些地方和别的浏览器不一致,比如要在js里移除一个html标签, 如果是IE,document.getElementById("a" ...

  4. Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.

    svn更新代码后,打开xcode工程文件,会出现  xxx..xcodeproj  cannot be opened because the project file cannot be parsed ...

  5. iPhone 上拨号键盘的发音规律

    上个学期在信号处理课上选做的题目和这个问题正好相关. 如焕杰同学所说,iPhone拨号音是使用双音多频信号(DTMF).双音多频信号是贝尔实验室发明的,通常用于发送被叫号码,它取代了早起的脉冲拨号方法 ...

  6. leetcode 153. Find Minimum in Rotated Sorted Array --------- java

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. psp0

    周活动总结表 姓名:苗堃                                                                                         ...

  8. ISBN号码

    总时间限制:  1000ms 内存限制:  65536kB 描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如"x-xxx- ...

  9. MySQL中日期与字符串相互转换,并进行日期比较查询

    技术交流群:233513714 1.日期无需转换查询(日期在数据库中的类型为字符串) select * from day where dateTime > '2016-03-15' 2.使用da ...

  10. kuangbin_ShortPath L (POJ 2502)

    dij部分还是跟模板差不多的 但是这题的难点是处理输入 或者说理解题意 事实上每个点之间都是可以走的......WA了好几发就因为没意识到同一条路线上的各个站点之间居然也可以走得比车子快.... PS ...