简体   繁体   中英

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. 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. Did I really found a bug in Java or missing something here?

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

 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. But Math.round(float) returns int. By default, your code was using Math.round(float) & the result value was out of int range. 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! Your numerical data are whole numbers (numbers without a decimal). As a result, the Math.round function will not work! So, to use the Math.round function correctly, you have to have decimal numericals as your data.

To illustrate this, I have written your code and added decimals to showcase you the optimal output of the Math.round() on Java:

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:

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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