Math.floor,Math.ceil,Math.rint,Math.round用法
阿新 • • 發佈:2018-01-20
返回 pos 數字 tint log 函數參數 get ron 等於
一、Math.floor函數講解
floor原意:地板。Math.floor函數是求一個浮點數的地板,就是求一個最接近它的整數,它的值小於或等於這個浮點數。看下面的例子:
package com.qiyuan.util; public class GetInt { /** * Math.floor函數測試 * @param args */ public static void main(String[] args) { System.out.println("Math.floor(-1.1): " + Math.floor(-1.1)); System.out.println("Math.floor(-1.5): " + Math.floor(-1.5)); System.out.println("Math.floor(-1.6): " + Math.floor(-1.6)); System.out.println("Math.floor(0.1): " + Math.floor(0.1)); System.out.println("Math.floor(0.5): " + Math.floor(0.5)); System.out.println("Math.floor(0.6): " + Math.floor(0.6)); System.out.println("Math.floor(1.1): " + Math.floor(1.1)); System.out.println("Math.floor(1.5): " + Math.floor(1.5)); System.out.println("Math.floor(1.6): " + Math.floor(1.6)); } }
結果為:
Math.floor(-1.1): -2.0 Math.floor(-1.5): -2.0 Math.floor(-1.6): -2.0 Math.floor(0.1): 0.0 Math.floor(0.5): 0.0 Math.floor(0.6): 0.0 Math.floor(1.1): 1.0 Math.floor(1.5): 1.0 Math.floor(1.6): 1.0
二、Math.ceil函數講解
ceil願意:天花板。Math.ceil函數執行的是向上取整計算,它返回的是大於或等於函數參數,並且與之最接近的整數。看下面的例子:
package com.qiyuan.util; public class GetInt { /** * Math.ceil函數測試 * @param args */ public static void main(String[] args) { System.out.println("Math.ceil(-1.1): " + Math.ceil(-1.1)); System.out.println("Math.ceil(-1.5): " + Math.ceil(-1.5)); System.out.println("Math.ceil(-1.6): " + Math.ceil(-1.6)); System.out.println("Math.ceil(0.1): " + Math.ceil(0.1)); System.out.println("Math.ceil(0.5): " + Math.ceil(0.5)); System.out.println("Math.ceil(0.6): " + Math.ceil(0.6)); System.out.println("Math.ceil(1.1): " + Math.ceil(1.1)); System.out.println("Math.ceil(1.5): " + Math.ceil(1.5)); System.out.println("Math.ceil(1.6): " + Math.ceil(1.6)); } }
結果為:
Math.ceil(-1.1): -1.0 Math.ceil(-1.5): -1.0 Math.ceil(-1.6): -1.0 Math.ceil(0.1): 1.0 Math.ceil(0.5): 1.0 Math.ceil(0.6): 1.0 Math.ceil(1.1): 2.0 Math.ceil(1.5): 2.0 Math.ceil(1.6): 2.0
三、Math.rint函數講解
Math.rint函數返回最接近參數的整數,如果有2個數同樣接近,則返回偶數的那個。
/** * Math.rint函數測試 * @param args */ public static void main(String[] args) { System.out.println("Math.rint(-1.1): " + Math.rint(-1.1)); System.out.println("Math.rint(-1.5): " + Math.rint(-1.5)); System.out.println("Math.rint(-1.6): " + Math.rint(-1.6)); System.out.println("Math.rint(0.1): " + Math.rint(0.1)); System.out.println("Math.rint(0.5): " + Math.rint(0.5)); System.out.println("Math.rint(0.6): " + Math.rint(0.6)); System.out.println("Math.rint(1.1): " + Math.rint(1.1)); System.out.println("Math.rint(1.5): " + Math.rint(1.5)); System.out.println("Math.rint(1.6): " + Math.rint(1.6)); }
結果為:
Math.rint(-1.1): -1.0 Math.rint(-1.5): -2.0 Math.rint(-1.6): -2.0 Math.rint(0.1): 0.0 Math.rint(0.5): 0.0 Math.rint(0.6): 1.0 Math.rint(1.1): 1.0 Math.rint(1.5): 2.0 Math.rint(1.6): 2.0
四、Math.round函數講解
Math.round方法,它表示"四舍五入",算法為Math.floor(x+0.5),即將原來的數字加上0.5後再向下取整
/** * Math.round函數測試 * @param args */ public static void main(String[] args) { System.out.println("Math.round(-1.1): " + Math.round(-1.1)); System.out.println("Math.round(-1.5): " + Math.round(-1.5)); System.out.println("Math.round(-1.6): " + Math.round(-1.6)); System.out.println("Math.round(0.1): " + Math.round(0.1)); System.out.println("Math.round(0.5): " + Math.round(0.5)); System.out.println("Math.round(0.6): " + Math.round(0.6)); System.out.println("Math.round(1.1): " + Math.round(1.1)); System.out.println("Math.round(1.5): " + Math.round(1.5)); System.out.println("Math.round(1.6): " + Math.round(1.6)); }
結果為:
Math.round(-1.1): -1 Math.round(-1.5): -1 Math.round(-1.6): -2 Math.round(0.1): 0 Math.round(0.5): 1 Math.round(0.6): 1 Math.round(1.1): 1 Math.round(1.5): 2 Math.round(1.6): 2
Math.floor,Math.ceil,Math.rint,Math.round用法