Math.round()
在 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()的更多相关文章
- Javascript四舍五入(Math.round()与Math.pow())
代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...
- 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( ...
- Math.Round四舍五入
Math.Round函数四舍五入的问题 今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...
- 【JAVA】Math.Round()函数常见问题“四舍5入”
java.lang.Math.Round()使用时候,处理方式整理,方便以后查找 /** * 测试函数 2014-01-10 */ public class TestMath { pu ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结
Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...
- C#中Math.Round()实现中国式四舍五入
C#中Math.Round()实现中国式四舍五入 C#中的Math.Round()并不是使用的"四舍五入"法.其实在VB.VBScript.C#.J#.T-SQL中Round函数都 ...
- Math.Round函数详解
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
- Math.Round函數
Math.Round這個函數的解釋是將值按指定的小數位數舍入,但並不就是四捨五入.這種舍入有時稱為就近舍入或四舍六入五成雙 其實在 VB, VBScript, C#, J#, T-SQL 中 Roun ...
- .Net中Math.Round与四舍五入
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
- C# Math.Round中国式的四舍五入法
double dou = 1.255; //这种是错误的 double dou_result = Math.Round(dou, 2); //结果: 1.25 dou_result = Math.Ro ...
随机推荐
- Git 将本地库添加到远程仓库
git remote add origin ssh://admin@127.0.0.1:29418/Prjs/prj1.git git push -u origin master
- EF批量插入数据(Z.EntityFramework.Extensions)
EF用原生的插入数据方法DbSet.ADD()和 DbSet.AddRange()都很慢.所以要做大型的批量插入只能另选它法. 1.Nugget 2.代码 using EF6._0Test.EF; u ...
- 【bzoj1042】[HAOI2008]硬币购物 背包dp+容斥原理
题解: 计数题 首先考虑容斥 这题很明显加了限制状态就很多 考虑没有限制 显然可以直接dp 然后 我们看一下 容斥 某一个使用>=k张 那么其实就是 f[i-k*c[]] 于是这样就可以做了
- 第二种掌握的排序Q-Q
#include<stdio.h> int main() { int s[10000]={0}; int i=0,j=0,n=0,x=0; scanf(" ...
- Flink--sink到kafka
package com.flink.DataStream import java.util.Properties import org.apache.flink.api.common.serializ ...
- jmeter访问mysql数据库
jdbc:mysql://localhost:3306/jy?allowMultiQueries=true 如果想同时执行多条语句
- BZOJ2178 圆的面积并 计算几何 辛普森积分
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2178.html 题目传送门 - BZOJ2178 题意 给出 $n(n\leq 1000)$ 个圆,求 ...
- Best Reward 拓展kmp
Problem Description After an uphill battle, General Li won a great victory. Now the head of state de ...
- java添加多个水印
package com.zhx.util.imgutil; import com.zhx.util.stringutil.ArithUtil; import net.coobird.thumbnail ...
- Android Studio项目生成Jar包
步骤: 1)在module的gradle文件中,将apply plugin:'com.android.application'改为apply plugin:'com.android.library' ...