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. 【转发】构建高可伸缩性的WEB交互式系统(中)

    原文转自:http://kb.cnblogs.com/page/503953/ 在<构建高可伸缩性的WEB交互式系统>的第一篇,我们介绍了Web交互式系统中平台的可伸缩性.本文将描述模块的 ...

  2. Ensemble Approaches分类技术

    所谓ensemble learning,简单来说,是指综合多种基础模型或弱分类器来完成最终的决策的机器学习方法. Bagging和Boosting(之前所提到的Adaboost)都属于这类方法. Co ...

  3. 用python matplotlib 画图

    state-machine environment object-oriente interface figure and axes backend and frontend user interfa ...

  4. Matlab 2012a 下载和安装教程

    迅雷下载地址    http://bbs.pinggu.org/thread-1426998-1-1.html(下载速度比较快) 1 Setup.exe 2. 不使用 internet 安装 then ...

  5. Linux档案与目录的管理

    本篇随笔中,主要介绍在Linux环境下,与档案和目录的管理相关的一些命令使用,具体包括如下几个方面: 目录的相关操作:cd,pwd,mkdir,rmdir(rm) 档案与目录的查视:ls 复制.删除与 ...

  6. 279. Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  7. Sprint第二个冲刺(第二天)

    一.Sprint 计划会议:      在这次会议中我们主要讨论了我们正在做的几个任务,比如说在美化按钮和增添图片上我们都发表了自己的想法,卓炜杰同学也把我们的想法进行分析,寻求最适合的方法.在查看用 ...

  8. 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  9. 设置只为View加一条边框,子视图大小超出父视图大小,边框在子视图下边显示

    #import "ViewController.h" @interface ViewController () @property (strong,nonatomic) UIVie ...

  10. TCP通讯程序设计---5

    这里主要包含客户机和服务器的编程. 一.编程模型函数化 使用函数说明:socket的理解 服务器: 创建socket使用函数----->socket 绑定地址使用函数------->bin ...