繁体   English   中英

Java:Math.round function 不适用于 integer

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

看起来 java 的Math.round function 在将 integer 值传递给它时存在问题。 我为几个输入运行它,但给出了令人惊讶的错误结果。

示例代码:

    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));
    }

标准输出:

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

我的用例是循环System.currentTimeMillis()/1000但最终得到错误的结果。 我真的在 Java 中发现了错误还是在这里遗漏了什么?

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));
}

您需要使用如上所示的双精度值。 Math.round(double) 返回长。 但是 Math.round(float) 返回 int。 默认情况下,您的代码使用 Math.round(float) 并且结果值超出了 int 范围。 那是问题所在

它在您的情况下不起作用的原因是因为舍入 function 只有在您的数据是小数时才能最佳工作! 您的数值数据是整数(没有小数点的数字)。 结果,Math.round function 将不起作用! 因此,要正确使用 Math.round function,您必须将十进制数字作为数据。

为了说明这一点,我编写了您的代码并添加了小数位以向您展示 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));
}

尝试使用java.time.Instant来完成系统时间的舍入,如下所示:

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

和一个演示程序:

// 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) {}
}

印刷:

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