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 ...
随机推荐
- hdu1166敌兵布阵_线段树单点更新
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
- Android动态Java代码调整window大小
Android调整window大小 举一个例子,设置当前的APP所需要的屏幕高度为设备高度的一半: Window window = getActivity().getWindow(); WindowM ...
- C# string格式的日期时间字符串转为DateTime类型
(1 )Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss (2):Convert.ToDateTime(string, IFo ...
- 为什么要urlencode
为什么要urlencode 1.为了正常获取值 字符 特殊字符的含义 URL编码 & 分隔不同的变量值对 %26 = 用来连接键和值 %3D ? 表示查询字符串的开始 %3F # 用来标志 ...
- C#山寨版本【天翼拨号客户端】---内含详细抓包,模拟数据---万事俱备,只欠东风。
官方的客户端的最大缺点: 1.一台电脑不允许使用同时启动多个网卡(目的是禁止使用虚拟WIFI或通过网卡后共享网络到路由器?): 2.使用路由器无法拨号(提示:不允许NAT后登录) 3.之前用某哥们破解 ...
- LeetCode Sort Colors (技巧)
题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...
- Aborting commit: 'XXXXXXXX'remains in conflict错误
今天在提交项目文件到本地SVN时提示错误如下: 过期:”global.php“在事务”21-1“, You have to update your working copy first. 运行upda ...
- Nginx 的线程池与性能剖析
http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt158 正如我们所知,NGINX采用了异步.事件驱动的方法来处理连接.这种处理方 ...
- Prepared Java infrastructure for distributed scenarios
code is sited on: https://github.com/zhoujiagen/javaiospike progress 2015/5/27 Nio/Nio2 examples, us ...
- 带同时滚动小色条的banner轮播图jq
<div class="baoliao tongcheng"> <p class="headline1">同城<font>活 ...