方法round返回最接近的long或int,由方法返回類型給出。
long round(double d) int round(float f)
此方法返回參數(shù)中最接近的 long 或 int ,如方法的返回類型所示。
下面是一個使用這個方法的例子 -
class Example { 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)); } }
當我們運行上面的程序,我們將得到以下結(jié)果 -
101 101 100 90
更多建議: