JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结
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 |
测试程序如下:
- public class MyTest {
- public static void main(String[] args) {
- double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
- for (double num : nums) {
- test(num);
- }
- }
- private static void test(double num) {
- System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
- System.out.println("Math.round(" + num + ")=" + Math.round(num));
- System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
- }
- }
运行结果
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的总结的更多相关文章
- Android java处理保留小数点后几位
方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = ...
- JAVA除法保留小数点后两位的两种方法
1.(double) (Math.round(sd3*10000)/10000.0); 这样为保持4位 (double) (Math.round(sd3*100)/100.0); 这样为保持2位 ...
- Java中保留小数点后几位
不想多说啥了..ε=(´ο`*)))唉 基础都给忘了..今天比赛 跌入十八层地狱.... 用DecimalFormat对象的format方法进行格式化.. package cn.test; impo ...
- php number_format()保留小数点后几位
[PHP_保留两位小数的相关函数] php保留两位小数并且四舍五入 Php代码 1 $num = 123213.666666; 2 echo sprintf("%.2f ...
- js保留小数点后N位的方法介绍
js保留小数点后N位的方法介绍 利用toFixed函数 代码如下 复制代码 <script language="javascript"> document.write( ...
- php number_format()保留小数点后几位有效数的函数 千位分组来格式化数字(转)
PHP保留小数点后2位的函数number_format number_format(带小数点的书,小数点后保留的位数) number_format(8.3486,2); //取得小数点后2位有效数/ ...
- C#保留小数点后几位
String.Format("{0:N1}", a) 保留小数点后一位 String.Format("{0:N2}", a) 保留小数点后两位 String.F ...
- 实现js保留小数点后N位的代码
在JS中,一般实现保留小数点后N位的话,都是利用toFixed函数 <script language="javascript"> document.write(&quo ...
- 格式化 float 类型,保留小数点后1位
""" 练习 : 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点, 并用字符串格式化显示出'xx.x%',只保留小数点后1位: &qu ...
随机推荐
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
- 重学STM32---(四)
今天把定时器看了一遍,觉得很有必要把记下来时常看一看 定时器3初始化,很简单,开时钟,装载ARR,PSC寄存器就行了,想要开什么中断在DIER寄存器设置就行了(不过当时我花了很长时间,用寄存器真的不熟 ...
- JS操作DOM常用API总结
<JS高程>中的DOM部分写的有些繁琐,还没勇气整理,直到看到了这篇博文 Javascript操作DOM常用API总结,顿时有了一种居高临下,一览全局的感觉.不过有时间还是得自己把书里面的 ...
- msp430 问题及解决记录
----------------------------- 2015.4.28 问题:开发板串口显示的内容为乱码 解决:使用的是原先产品主板的15200的波特率设置,但看来或者是开发板不支持11520 ...
- 帝国cms缩略图:网站不同地方生成不同的缩略图
本文转自:http://blog.sina.com.cn/s/blog_4d49ba58010115sd.html 方便网站多地方调用不同尺寸的缩略图. 根据图片源,在网站任意位置生成不同大小的缩略图 ...
- php 在客户端生成txt文件并且下载
在访问php时生成txt文件 $filename = 'file.text'; //也可以是其他后缀格式的 $ua = $_SERVER["HTTP_USER_AGENT"]; f ...
- UVa 12558 - Egyptian Fractions (HARD version)
题目大意: 给出一个真分数,把它分解成最少的埃及分数的和.同时给出了k个数,不能作为分母出现,要求解的最小的分数的分母尽量大. 分析: 迭代加深搜索,求埃及分数的基础上,加上禁用限制就可以了.具体可以 ...
- 让Grub 2来拯救你的 bootloader
没有什么事情比 bootloader 坏掉更气人的了,充分发挥 Grub 2 的作用,让 bootloader 安分工作吧.为什么这么说? Grub 2 是最受欢迎的 bootloader ,几乎用在 ...
- makefile--统一目标输出目录 (六)
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 上一节我们把规则单独提取出来,方便了Makefile的维护,每个模块只需要给出关于自己的一些变量 ...
- 1-1 Windows应用程序的特点
主要内容:介绍Windows应用程序的特点,并附加了消息和事件的一些区别 //以后该分类中字体均采用 隶书 4(14pt) 1. 面向对象 <1>针对Windows应用本身,如记事本界面, ...