Java round() 方法
rint() 方法返回一個最接近的int、long型值。
語法
該方法有以下幾種語法格式:
long round(double d) int round(float f)
參數
d -- double或float的原生數據類型
f -- float原生數據類型
返回值
返回一個最接近的int、long型值,方法會指定返回的數據類型。
實例
public class Test{ public static void main(String args[]){ double d = 100.675; double e = 100.500; float f = 100; float g = 90f; System.out.println(Math.round(d)); System.out.println(Math.round(e)); System.out.println(Math.round(f)); System.out.println(Math.round(g)); } }
編譯以上程序,輸出結果為:
101 101 100 90