Java - 即使十进制数低于5,也要向上编译

时间:2018-02-08 12:07:33

标签: java math

我有这个:

return ((Double) Math.ceil(Integer.parseInt(matcher.group()) / 10)).intValue();

我需要一个数字,即使十进制数低于5,也可以上传

使用此代码,我有以下示例:

((Double) Math.ceil(14 / 10).intValue() = 1
((Double) Math.ceil(26 / 10).intValue() = 3
((Double) Math.ceil(25 / 10).intValue() = 3

我需要:

((Double) Math.ceil(14 / 10).intValue() = 2
((Double) Math.ceil(26 / 10).intValue() = 3
((Double) Math.ceil(25 / 10).intValue() = 3

3 个答案:

答案 0 :(得分:5)

除以整数可以得到整数。 Math.ceil()没有任何内容可做,因为您获得的数字不是1.4,它是1.在分割之前将至少一个数字转换为双倍:

e.g。

((Double) Math.ceil(Integer.parseInt(matcher.group()) / 10.)).intValue();

((Double) Math.ceil(Double.parseDouble(matcher.group()) / 10.)).intValue();

答案 1 :(得分:1)

将int div更改为double division,例如14 / 10D

目前您的14/10结果为1 ...

答案 2 :(得分:1)

return (int) Math.ceil(Integer.parseInt(matcher.group()) / 10.0));

另外注意不要使用整数除法,26/10 = 2.