JAva Mathクラスの3つの整数関数

1202 ワード

floorが返される最大整数roundは4捨5入の計算であり、入るときはその整数より大きい(-1.5の場合、4捨5入後の結果は期待されず、解決策はまず彼に絶対値を取り、round法を用いる)
roundメソッドは、「四捨五入」を表し、アルゴリズムはMath.floor(x+0.5)であり、元の数字に0.5を加えてから下に整列するので、Math.round(11.5)の結果は12、Math.round(-11.5)の結果は-11である.
Ceilは彼の最小整数以上です
package com.test.hehe;

public class MathTest {
	public static void main(String args[]){
		double[] arrays = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
		for(Object num : arrays){
			System.out.println("Math.floor(" + num + ")=" + Math.floor((double) num));   
		    System.out.println("Math.round(" + num + ")=" + Math.round((double) num));   
		    System.out.println("Math.ceil(" + num + ")=" + Math.ceil((double) 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