简体   繁体   English

Java:Math.round function 不适用于 integer

[英]Java: Math.round function not working with integer

Looks like there is issue with java's Math.round function, when passing integer value to it.看起来 java 的Math.round function 在将 integer 值传递给它时存在问题。 I ran it for couple of inputs but giving suprisingly wrong results.我为几个输入运行它,但给出了令人惊讶的错误结果。

Sample Code:示例代码:

    public static void main(String[] args) {
        System.out.println("roundOff1: " + Math.round(1669053278));
        System.out.println("roundOff2: " + Math.round(1669053304));
        System.out.println("roundOff3: " + Math.round(1669053314));
        System.out.println("roundOff4: " + Math.round(1669053339));
    }

Stdout:标准输出:

roundOff1: 1669053312
roundOff2: 1669053312
roundOff3: 1669053312
roundOff4: 1669053312

My use case was to round of the System.currentTimeMillis()/1000 but end in getting wrong result.我的用例是循环System.currentTimeMillis()/1000但最终得到错误的结果。 Did I really found a bug in Java or missing something here?我真的在 Java 中发现了错误还是在这里遗漏了什么?

int values dont have decimal places so they cant be rounded so u need to add an extra parametre to round method eg to round to nearest 10 you can use int 值没有小数位,所以它们不能四舍五入,所以你需要向 round 方法添加一个额外的参数,例如四舍五入到最接近的 10,你可以使用

 Math. round(num/10.0) * 10;
public static void main(String[] args) {

    System.out.println("roundOff1: " + Math.round(1669053278d));
    System.out.println("roundOff2: " + Math.round(1669053304d));
    System.out.println("roundOff3: " + Math.round(1669053314d));
    System.out.println("roundOff4: " + Math.round(1669053339d));
}

You need to use double value as shown above.您需要使用如上所示的双精度值。 Math.round(double) return long. Math.round(double) 返回长。 But Math.round(float) returns int.但是 Math.round(float) 返回 int。 By default, your code was using Math.round(float) & the result value was out of int range.默认情况下,您的代码使用 Math.round(float) 并且结果值超出了 int 范围。 That was the issue那是问题所在

The reason why it does not work in your case is because the rounding function can only optimally work if your data is a decimal!它在您的情况下不起作用的原因是因为舍入 function 只有在您的数据是小数时才能最佳工作! Your numerical data are whole numbers (numbers without a decimal).您的数值数据是整数(没有小数点的数字)。 As a result, the Math.round function will not work!结果,Math.round function 将不起作用! So, to use the Math.round function correctly, you have to have decimal numericals as your data.因此,要正确使用 Math.round function,您必须将十进制数字作为数据。

To illustrate this, I have written your code and added decimals to showcase you the optimal output of the Math.round() on Java:为了说明这一点,我编写了您的代码并添加了小数位以向您展示 Java 上 Math.round() 的最佳 output:

import java.lang.Math; 
class RoundingNumbers{
public static void main(String[] args){
double first_parameter = 1669053278.3;
System.out.println("roundOff1: " + Math.round(first_parameter));
double second_parameter = 1669053304.8;
System.out.println("roundOff2: " + Math.round(second_parameter));
double third_parameter = 1669053314.8;
System.out.println("roundOff3: " + Math.round(third_parameter));
double fourth_parameter = 1669053339.5;
System.out.println("roundOff4: " + Math.round(fourth_parameter));
}

Try using java.time.Instant to accomplish your rounding of system time as in:尝试使用java.time.Instant来完成系统时间的舍入,如下所示:

// the getEpochSecond() truncates - so add 500 millis first
long roundedSecs = Instant.now().plusMillis(500).getEpochSecond();

And a demo program:和一个演示程序:

// use a loop iterating every 250millis to demonstrate rounding

public static void main(String[] args) {
    try {
    for (int i = 0; i < 10; i++) {
        Instant nowTime = Instant.now();
        long b4 = nowTime.getEpochSecond();
        
        nowTime = nowTime.plusMillis(500);

       long result = nowTime.getEpochSecond();
       System.out.println("b4: "+b4+" after:"+result);
       Thread.sleep(250);
    }
    } catch (Exception e) {}
}

Prints:印刷:

b4: 1669056744 after:1669056744
b4: 1669056744 after:1669056745
b4: 1669056745 after:1669056745
b4: 1669056745 after:1669056746
b4: 1669056745 after:1669056746
b4: 1669056746 after:1669056746
b4: 1669056746 after:1669056746
b4: 1669056746 after:1669056747
b4: 1669056746 after:1669056747
b4: 1669056747 after:1669056747

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM