簡體   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