在Java中,可以使用Math.round()方法來(lái)對(duì)整數(shù)進(jìn)行四舍五入。例如:
int num = 5;
int roundedNum = Math.round(num);
System.out.println("Rounded number: " + roundedNum);
這將輸出:
Rounded number: 5
如果要對(duì)浮點(diǎn)數(shù)進(jìn)行四舍五入,可以先將浮點(diǎn)數(shù)轉(zhuǎn)換為整數(shù),然后再使用Math.round()方法。例如:
double num = 5.5;
int roundedNum = (int) Math.round(num);
System.out.println("Rounded number: " + roundedNum);
這將輸出:
Rounded number: 6