代碼如下,后面的注釋是輸出的結果
public static void main(String[] args) { System.out.println(Math.round(0.399));//0 System.out.println(Math.round(0.4));//0 System.out.println(Math.round(0.41));//0 System.out.println(Math.round(0.499));//0 System.out.println(Math.round(0.5));//1 System.out.println(Math.round(0.51));//1 System.out.println(Math.round(0.6));//1 System.out.println("======================"); System.out.println(Math.round(-0.6));//-1 System.out.println(Math.round(-0.51));//-1 System.out.println(Math.round(-0.5));//0 System.out.println(Math.round(-0.499));//0 System.out.println(Math.round(-0.41));//0 System.out.println(Math.round(-0.4));//0 System.out.println(Math.round(-0.399));//0 }
總結,round的進位是向數軸的右方向進位的,而不是按照數的絕對值進行四舍五入的
關於Math對三角函數表示的一些知識點查看https://blog.csdn.net/River_Continent/article/details/80637952