math round 用法(Math.round5.5等于多少)
【死记硬背】Math类提供了三个与取整有关的方法:ceil、floor和round,我来为大家讲解一下关于math round 用法?跟着小编一起来看一看吧!
math round 用法
【死记硬背】
Math类提供了三个与取整有关的方法:ceil、floor和round。
ceil:英文意思为天花板,表示向上取整,Math.ceil(11.3)的结果为12.0,Math.ceil(-11.3)的结果是-11.0;
floor:英文意思为地板,表示向下取整,Math.floor(11.6)的结果为11.0,Math.floor(-11.6)的结果是-12.0;
round:英文意思为原型的,表示四舍五入,Math.round(5.5)的结果为6,Math.round(-5.5)的结果为-5,
【答案解析】
示例代码如下:
public class Test{
public static void main(String[] args) {
double ceil1 = Math.ceil(11.3);
double ceil2 = Math.ceil(-11.3);
System.out.println("Math.ceil(11.3) = " ceil1);
System.out.println("Math.ceil(-11.3) = " ceil2);
double floor1 = Math.floor(11.6);
double floor2 = Math.floor(-11.6);
System.out.println("Math.floor(11.6) = " floor1);
System.out.println("Math.floor(-11.6) = " floor2);
long round1 = Math.round(5.5);
long round2 = Math.round(-5.5);
System.out.println("Math.round(5.5) = " round1);
System.out.println("Math.round(-5.5) = " round2);
}
}
// 输出内容如下
Math.ceil(11.3) = 12.0
Math.ceil(-11.3) = -11.0
Math.floor(11.6) = 11.0
Math.floor(-11.6) = -12.0
Math.round(5.5) = 6
Math.round(-5.5) = -5
Math.ceil的源码解析:
// Math.ceil的方法:
public static double ceil(double a) {
return StrictMath.ceil(a);
}
// StrictMath.ceil的方法:
public static double ceil(double a) {
return floorOrCeil(a, -0.0, 1.0, 1.0);
}
Math.floor的源码解析:
// Math.floor的方法:
public static double floor(double a) {
return StrictMath.floor(a);
}
// StrictMath.floor的方法
public static double floor(double a) {
return floorOrCeil(a, -1.0, 0.0, -1.0);
}
Math.ceil和Math.floor最终共用的方法:
// floorOrCeil的方法
private static double floorOrCeil(double a,
double negativeBoundary,
double positiveBoundary,
double sign) {
int exponent = Math.getExponent(a);
if (exponent < 0) {
/*
* Absolute value of argument is less than 1.
* floorOrceil(-0.0) => -0.0
* floorOrceil( 0.0) => 0.0
*/
return ((a == 0.0) ? a :
( (a < 0.0) ? negativeBoundary : positiveBoundary) );
} else if (exponent >= 52) {
/*
* Infinity, NaN, or a value so large it must be integral.
*/
return a;
}
// Else the argument is either an integral value already XOR it
// has to be rounded to one.
assert exponent >= 0 && exponent <= 51;
long doppel = Double.doubleToRawLongBits(a);
long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent;
if ( (mask & doppel) == 0L )
return a; // integral value
else {
double result = Double.longBitsToDouble(doppel & (~mask));
if (sign*a > 0.0)
result = result sign;
return result;
}
}
Math.round的源码解析:
public static long round(double a) {
long longBits = Double.doubleToRawLongBits(a);
long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
>> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
DoubleConsts.EXP_BIAS) - biasedExp;
if ((shift & -64) == 0) { // shift >= 0 && shift < 64
// a is a finite number such that pow(2,-64) <= ulp(a) < 1
long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
| (DoubleConsts.SIGNIF_BIT_MASK 1));
if (longBits < 0) {
r = -r;
}
// In the comments below each Java expression evaluates to the value
// the corresponding mathematical expression:
// (r) evaluates to a / ulp(a)
// (r >> shift) evaluates to floor(a * 2)
// ((r >> shift) 1) evaluates to floor((a 1/2) * 2)
// (((r >> shift) 1) >> 1) evaluates to floor(a 1/2)
return ((r >> shift) 1) >> 1;
} else {
// a is either
// - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
// - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
// - an infinity or NaN
return (long) a;
}
}
【温馨提示】
点赞 收藏文章,关注我并私信回复【面试题解析】,即可100%免费领取楼主的所有面试题资料!
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com