13,100
社区成员
发帖
与我相关
我的任务
分享
public static int sgn(int num){
if(num==0)
return 0;
return num>0?1:-1;
}
public class MathTest {
public static void main(String[] args) {
System.out.println("小数点后第一位=5");
System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));
System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));
System.out.println();
System.out.println("小数点后第一位<5");
System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));
System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));
System.out.println();
System.out.println("小数点后第一位>5");
System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));
System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));
}
}
public static int round(float a) {
return (int)floor(a + 0.5f);
}
public static long round(double a) {
return (long)floor(a + 0.5d);
}