round public static int round(float a)返回最接近参数的 int。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 int 类型。换句话说,结果等于以下表达式的值: (int)Math.floor(a + 0.5f)怎么算都说明白了,怎么很"官方"呢?
按书上的说法就是: switch (expression) { case n: statements case m: statements . . . default: statements } The expression must either be of an integer type (char, byte, short, or int, or a corresponding wrapper class) or an enum type.
啊 这是哪本书啊 这么权威 我都忘了枚举了 我一定要看看zang说一下书名吧 然后有没有中文版
The Java Programming Language 第四版 有中文版的.
有问题 最开始那个问题API解释的意思完全不对了是不是应该改成这样public static int round(float a)返回最接近参数的 int。结果将参数舍入为整数,加上 1/2(0.5),对结果调用 floor 并将所得结果强制转换为 int 类型。少了一个词 逗号写成了冒号是不是这样
java,Math得四舍五入是错误的 现在有人还在用round? 正确做法是 public int getRound(double dSource){ int iRound //BigDecimal的构造函数参数类型是 double BigDecimal deSource = new BigDecimal(dSource); //deSource.setScale(0,BigDecimal.ROUND_HALF_UP) 返回值类型 BigDecimal //intValue() 方法将BigDecimal转化为int iRound= deSource.setScale(0,BigDecimal.ROUND_HALF_UP).intValue(); return iRound; }
import java.math.BigDecimal;public class Test { public static double getRound(double dSource, int iRound){ BigDecimal deSource = new BigDecimal(dSource); return deSource.setScale(iRound, BigDecimal.ROUND_HALF_UP).doubleValue(); }
public static int round(float a)返回最接近参数的 int。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 int 类型。换句话说,结果等于以下表达式的值: (int)Math.floor(a + 0.5f)怎么算都说明白了,怎么很"官方"呢?
switch (expression) {
case n: statements
case m: statements
. . .
default: statements
}
The expression must either be of an integer type (char, byte, short, or int, or a corresponding wrapper class) or an enum type.
第四版
有中文版的.
最开始那个问题API解释的意思完全不对了是不是应该改成这样public static int round(float a)返回最接近参数的 int。结果将参数舍入为整数,加上 1/2(0.5),对结果调用 floor 并将所得结果强制转换为 int 类型。少了一个词 逗号写成了冒号是不是这样
floor就返回靠近正坐标的数啦
现在有人还在用round?
正确做法是
public int getRound(double dSource){
int iRound //BigDecimal的构造函数参数类型是
double BigDecimal deSource = new BigDecimal(dSource);
//deSource.setScale(0,BigDecimal.ROUND_HALF_UP) 返回值类型 BigDecimal
//intValue() 方法将BigDecimal转化为int
iRound= deSource.setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
return iRound;
}
BigDecimal deSource = new BigDecimal(dSource);
return deSource.setScale(iRound, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static void main(String args[]) { System.out.println(getRound(-11.5555555, 0));
System.out.println(getRound(-11.5555555, 1));
System.out.println(getRound(-11.5555555, 2));
}
}
修改后的代码