詳解Java中Math.round()的取整規(guī)則
做Java的面試題時(shí)遇到了以下這題,百度了一下Math.round()的修約規(guī)則,有的說是四舍五入,有的說是四舍六入,發(fā)現(xiàn)和我學(xué)分析化學(xué)時(shí)用的數(shù)字修約規(guī)則(四舍六入五成雙)很像,所以驗(yàn)證一下;
原題:Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
作者給的解題方法如下:
答:Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在參數(shù)上加0.5然后進(jìn)行下取整。
先說結(jié)論,題目作者給的解釋是對的,后來找了該方法的定義,結(jié)果方法的定義就是這個(gè)原理,果然看文檔才是王道;
round方法:
static long round(double a)
此方法返回的參數(shù)最接近的long.
static int round(float a)
此方法返回的參數(shù)最接近的整數(shù).
注:四舍六入五成雙:
當(dāng)有效位數(shù)確定后,其后面多余的數(shù)字應(yīng)該舍去,只保留有效數(shù)字最末一位,這種修約(舍入)規(guī)則是“四舍六入五成雙”,也即“4舍6入5湊偶”這里“四”是指≤4 時(shí)舍去,”六”是指≥6時(shí)進(jìn)上,”五”指的是根據(jù)5后面的數(shù)字來定,當(dāng)5后有數(shù)時(shí),舍5入1;當(dāng)5后無有效數(shù)字時(shí),需要分兩種情況來講:①5前為奇數(shù),舍5入1;②5前為偶數(shù),舍5不進(jìn)。(0是偶數(shù))
以下只論證static int round(float a)
//四舍 int[] test1 = { Math.round(2.40f), Math.round(2.44f), Math.round(2.45f), Math.round(2.46f), Math.round(-2.40f), Math.round(-2.44f), Math.round(-2.45f), Math.round(-2.46f), Math.round(3.40f), Math.round(3.44f), Math.round(3.45f), Math.round(3.46f), Math.round(-3.40f), Math.round(-3.44f), Math.round(-3.45f), Math.round(-3.46f)}; for(int i = 0; i< test1.length; i++) { System.out.print(test1[i]+","); } //輸出:2,2,2,2,-2,-2,-2,-2,3,3,3,3,-3,-3,-3,-3,符合四舍;也符合 加0.5,進(jìn)行下取整; //六入 int[] test2 = { Math.round(2.60f), Math.round(2.64f), Math.round(2.65f), Math.round(2.66f), Math.round(-2.60f), Math.round(-2.64f), Math.round(-2.65f), Math.round(-2.66f), Math.round(3.60f), Math.round(3.64f), Math.round(3.65f), Math.round(3.66f), Math.round(-3.60f), Math.round(-3.64f), Math.round(-3.65f), Math.round(-3.66f)}; for(int i = 0; i< test2.length; i++) { System.out.print(test2[i]+","); } //輸出:3,3,3,3,-3,-3,-3,-3,4,4,4,4,-4,-4,-4,-4,符合六入;也符合 加0.5,進(jìn)行下取整; //五成雙之五后無數(shù)字 int[] test3 = { Math.round(2.5f), Math.round(-2.5f), Math.round(3.5f), Math.round(-3.5f)}; for(int i = 0; i< test3.length; i++) { System.out.print(test3[i]+","); } //輸出:3,-2,4,-3,不符合五成雙;符合 加0.5,進(jìn)行下取整; //五成雙之五后有數(shù)字(零,非零) int[] test4 = { Math.round(2.50f), Math.round(2.51f), Math.round(2.59f), Math.round(-2.50f), Math.round(-2.51f), Math.round(-2.59f), Math.round(3.50f), Math.round(3.51f), Math.round(3.59f), Math.round(-3.50f), Math.round(-3.51f), Math.round(-3.59f), }; for(int i = 0; i< test4.length; i++) { System.out.print(test4[i]+","); } //輸出:3,3,3,-2,-3,-3,4,4,4,-3,-4,-4,不符合五后非零進(jìn)一;符合 加0.5,進(jìn)行下取整; //結(jié)論:Math.round()的取整規(guī)則不符合四舍六入五成雙,以上案例符合 加0.5,進(jìn)行下取整;
到此這篇關(guān)于詳解Java中Math.round()的取整規(guī)則的文章就介紹到這了,更多相關(guān)Java Math.round()取整 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot 實(shí)現(xiàn)配置多個(gè)DispatcherServlet最簡單方式
這篇文章主要介紹了spring boot 實(shí)現(xiàn)配置多個(gè)DispatcherServlet最簡單方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01一文搞明白Java?Spring?Boot分布式事務(wù)解決方案
這篇文章主要介紹了一文搞明白Java?Spring?Boot分布式事務(wù)解決方案,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07http中g(shù)et請求與post請求區(qū)別及如何選擇
這篇文章主要介紹了http中g(shù)et請求與post請求在應(yīng)用中應(yīng)該如何選擇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-09-09BiConsumer接口中的方法andThen?accept使用詳解
這篇文章主要為大家介紹了BiConsumer接口中的方法andThen?accept使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07