亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JAVA中取整數(shù)的4種方法總結(jié)

 更新時(shí)間:2023年07月25日 09:33:57   作者:飛得更高100  
這篇文章主要給大家介紹了關(guān)于JAVA中取整數(shù)的4種方法,在java的Math類(lèi)中,提供了許許多多的和數(shù)學(xué)計(jì)算有關(guān)的方法,其中也包括取整的,需要的朋友可以參考下

1.向下取整

Math.floor(),向下取整就是取最小的整數(shù),如1.9就返回值為1.0,-1.9就返回-2.0,返回的總是小于等于原數(shù)。

2.向上取整

Math.ceil(),向上取整顧名思義就是取最大的整數(shù),如1.9就返回2.0,-1.9就返回-1.0,返回的總是大于等于原數(shù),如圖。

3.接近取整

Math.rint(),接近取整顧名思義就是接近哪個(gè)取整哪個(gè),如1.6接近2,所以就取2;1.4接近1,所以就取1;那么1.5呢,1.5跟1和2都很接近,這時(shí)候就取偶數(shù),如圖。

4.四舍五入或(+0.5向下取整)

Math.round(),這個(gè)round就有點(diǎn)意思了,如果只考慮正整數(shù)的情況下就很簡(jiǎn)單,就是我們平時(shí)說(shuō)的四舍五入來(lái)算就行了,如果是負(fù)數(shù),那么的話就要負(fù)數(shù)+0.5然后再向下取整,如Math.round(-0.6) = (-0.6+0.5)=-0.1,然后向下取整就是-1,

5.類(lèi)型強(qiáng)轉(zhuǎn)(int)double,(int) float......

注意:此種方法將會(huì)直接截取小數(shù)后面的部分,直接拿到整數(shù)。

public class demo_2 {
	public static void main(String[] args) {
		// 向下取整
		System.out.println(Math.floor(1.9));
		System.out.println(Math.floor(-1.9));
		System.out.println("--------");
		// 向上取整
		System.out.println(Math.ceil(1.9));
		System.out.println(Math.ceil(-1.9));
		System.out.println("--------");
		// 接近取整
		System.out.println(Math.rint(1.6));
		System.out.println(Math.rint(1.4));
		System.out.println(Math.rint(1.5));
		System.out.println(Math.rint(2.5));
		System.out.println("--------");
		// 四舍五入
		System.out.println(Math.round(2.5));
		System.out.println(Math.round(-2.5));
		System.out.println(Math.round(1.2));
	}
}

總結(jié)

到此這篇關(guān)于JAVA中取整數(shù)的4種方法的文章就介紹到這了,更多相關(guān)JAVA取整數(shù)方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論