您好,登錄后才能下訂單哦!
近期用到四舍五入想到以前整理了一點(diǎn),就順便重新整理好經(jīng)常見到的一些四舍五入,后續(xù)遇到常用也會(huì)直接在這篇文章更新。。。
public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//計(jì)算平方根 *Math.cbrt()//計(jì)算立方根 *Math.pow(a, b)//計(jì)算a的b次方 *Math.max( , );//計(jì)算最大值 *Math.min( , );//計(jì)算最小值 */ System.out.println(Math.sqrt(16)); //4.0 System.out.println(Math.cbrt(8)); //2.0 System.out.println(Math.pow(3,2)); //9.0 System.out.println(Math.max(2.3,4.5));//4.5 System.out.println(Math.min(2.3,4.5));//2.3 /** * abs求絕對(duì)值 */ System.out.println(Math.abs(-10.4)); //10.4 System.out.println(Math.abs(10.1)); //10.1 /** * ceil天花板的意思,就是返回大的值 */ System.out.println(Math.ceil(-10.1)); //-10.0 System.out.println(Math.ceil(10.7)); //11.0 System.out.println(Math.ceil(-0.7)); //-0.0 System.out.println(Math.ceil(0.0)); //0.0 System.out.println(Math.ceil(-0.0)); //-0.0 System.out.println(Math.ceil(-1.7)); //-1.0 /** * floor地板的意思,就是返回小的值 */ System.out.println(Math.floor(-10.1)); //-11.0 System.out.println(Math.floor(10.7)); //10.0 System.out.println(Math.floor(-0.7)); //-1.0 System.out.println(Math.floor(0.0)); //0.0 System.out.println(Math.floor(-0.0)); //-0.0 /** * random 取得一個(gè)大于或者等于0.0小于不等于1.0的隨機(jī)數(shù) */ System.out.println(Math.random()); //小于1大于0的double類型的數(shù) System.out.println(Math.random()*2);//大于0小于1的double類型的數(shù) System.out.println(Math.random()*2+1);//大于1小于2的double類型的數(shù) /** * rint 四舍五入,返回double值 * 注意.5的時(shí)候會(huì)取偶數(shù) 異常的尷尬=。= */ System.out.println(Math.rint(10.1)); //10.0 System.out.println(Math.rint(10.7)); //11.0 System.out.println(Math.rint(11.5)); //12.0 System.out.println(Math.rint(10.5)); //10.0 System.out.println(Math.rint(10.51)); //11.0 System.out.println(Math.rint(-10.5)); //-10.0 System.out.println(Math.rint(-11.5)); //-12.0 System.out.println(Math.rint(-10.51)); //-11.0 System.out.println(Math.rint(-10.6)); //-11.0 System.out.println(Math.rint(-10.2)); //-10.0 /** * round 四舍五入,float時(shí)返回int值,double時(shí)返回long值 */ System.out.println(Math.round(10.1)); //10 System.out.println(Math.round(10.7)); //11 System.out.println(Math.round(10.5)); //11 System.out.println(Math.round(10.51)); //11 System.out.println(Math.round(-10.5)); //-10 System.out.println(Math.round(-10.51)); //-11 System.out.println(Math.round(-10.6)); //-11 System.out.println(Math.round(-10.2)); //-10 } }
總結(jié)
以上所述是小編給大家介紹的Java中Math類常用方法代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留
言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。