在 JAVA 中四舍五入采用 Math.round(T a) 函数,函数返回的是一个 long 类型的长整型,参数 a 可以是 double 也可以是 float。

查看 JDK 源码:

  

public static long round(double a) {
if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
return (long)floor(a + 0.5d);
else
return 0;
}

  

public static double floor(double a) {
return StrictMath.floor(a); // default impl. delegates to StrictMath
}

  

 public static double floor(double a) {
return floorOrCeil(a, -1.0, 0.0, -1.0);
}

  

其实具体实现还是挺复杂的。

需要注意的是 a 为负数的情况。

 System.out.println("小数点后第一位=5");
System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));
System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));
System.out.println();
System.out.println("小数点后第一位<5");
System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));
System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));
System.out.println();
System.out.println("小数点后第一位>5");
System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));
System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));

输出结果是:

小数点后第一位=5
正数:Math.round(11.5)=12
负数:Math.round(-11.5)=-11 小数点后第一位<5
正数:Math.round(11.46)=11
负数:Math.round(-11.46)=-11 小数点后第一位>5
正数:Math.round(11.68)=12
负数:Math.round(-11.68)=-12

正数和我们平时学的一样,负数在小数点后第一位为5时,直接舍去小数部分,大于5时减一,小于5时直接舍去小数部分。

Math.round()的更多相关文章

  1. Javascript四舍五入(Math.round()与Math.pow())

    代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...

  2. Javascript Math.ceil()与Math.round()与Math.floor()区别

    Math.ceil()向上舍入 1 2 3 alert(Math.ceil(20.1)) //输出 21 alert(Math.ceil(20.5)) //输出 21 alert(Math.ceil( ...

  3. Math.Round四舍五入

    Math.Round函数四舍五入的问题   今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...

  4. 【JAVA】Math.Round()函数常见问题“四舍5入”

    java.lang.Math.Round()使用时候,处理方式整理,方便以后查找   /**  * 测试函数 2014-01-10  */ public class TestMath {     pu ...

  5. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结

    Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...

  6. C#中Math.Round()实现中国式四舍五入

    C#中Math.Round()实现中国式四舍五入 C#中的Math.Round()并不是使用的"四舍五入"法.其实在VB.VBScript.C#.J#.T-SQL中Round函数都 ...

  7. Math.Round函数详解

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  8. Math.Round函數

    Math.Round這個函數的解釋是將值按指定的小數位數舍入,但並不就是四捨五入.這種舍入有時稱為就近舍入或四舍六入五成雙 其實在 VB, VBScript, C#, J#, T-SQL 中 Roun ...

  9. .Net中Math.Round与四舍五入

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  10. C# Math.Round中国式的四舍五入法

    double dou = 1.255; //这种是错误的 double dou_result = Math.Round(dou, 2); //结果: 1.25 dou_result = Math.Ro ...

随机推荐

  1. PAT Basic 1073. 多选题常见计分法

    题目内容 多选题常见计分法(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 批改多选题是比较麻烦的事情,有 ...

  2. SQLServer 表连接种类

    SQLServer 有3种物理连接:Nested Loop(嵌套循环).Merge Join(合并联接).Hash Join(哈希联接). T-SQL中的inner/left/right/full j ...

  3. uva 11992

    题意: 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x ...

  4. JMeter上传案例2

    今天自己的QQ群里有个朋友一直在问JMeter图片上传的问题 原始通过JMeter抓包如下: 参考: http://blog.csdn.net/huashao0602/article/details/ ...

  5. Flink的Windows

    在讲解windows的众多操作之前,需要讲解一个概念: 源源不断的数据流是无法进行统计工作的,因为数据流没有边界,就无法统计到底有多少数据经过了这个流.也无法统计数据流中的最大值,最小值,平均值,累加 ...

  6. windows搭建RocketMQ服务

    写在前面: 1.基于rocketmq 4.2.0版本 2.64位 win10 系统 3.JDK 1.8 (注意,jdk路径一定不要有空格,不然执行相应的cmd命令时会报错) 4.其它需要的软件,mav ...

  7. .net core cookie登录和session的 DataProtectionProvider 加入 redis

    string redisConnectionString = Configuration.GetSection("Storage:Redis").GetValue<strin ...

  8. Badboy录制Jmter脚本

    提纲 1.特性和用途 2.下载和安装 3.界面介绍 4.录制脚本(注意:badboy默认是打开就开始录制,需要在step双击后进行取消默认设置) 5.添加断言(参数化设置,注意:badboy默认只运行 ...

  9. js實現彈窗

    strSucc += "<br/><font color=\"red\">提醒您!在預設狀態下,Google Chrome 會阻止彈出式視窗自動在 ...

  10. sql的with as用法

    http://www.cnblogs.com/linjiqin/archive/2013/06/24/3152667.html