2010-04-16 48 views
80

現在,我想這樣的:四捨五入雙把它變成一個int(JAVA)

int a = round(n); 

其中ndouble,但它不工作。我究竟做錯了什麼?

+0

可能的重複http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – 2010-04-16 17:09:53

+4

你應該詳細說明「不工作」更多詳情。怎麼了?不會發生什麼?你期望什麼?你有什麼錯誤?你在同一個類中有'round()'方法嗎?你是否導入了靜態java.lang.Math。*'?等等。有很多方法可以對數字進行舍入,因此也有很多可能的答案。換句話說,你的問題含糊不清,不能以目前的形式合理回答。它在黑暗中拍攝。 – BalusC 2010-04-16 17:09:55

+1

「不工作」是指不是舍入到最接近的整數,或拋出異常,或不是向上舍入/舍入?這個問題是無用的,而不必交叉參考上下文與答案。 – modulitos 2014-07-11 21:25:09

回答

181

代碼段中round()方法的返回類型是什麼?

如果這是Math.round()方法,則在輸入參數爲Double時返回Long。

所以,你將不得不返回值的類型轉換:

int a = (int) Math.round(doubleVar); 
+0

考慮使用Math.toIntExact(long),而不是僅將其轉換爲int;一個double可以保存相當多的值,如果你的double比你想象的大,你可能不想默默地拋出最重要的位。 – othp 2018-03-01 16:38:43

3
import java.math.*; 
public class TestRound11 { 
    public static void main(String args[]){ 
    double d = 3.1537; 
    BigDecimal bd = new BigDecimal(d); 
    bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP); 
    // output is 3.15 
    System.out.println(d + " : " + round(d, 2)); 
    // output is 3.154 
    System.out.println(d + " : " + round(d, 3)); 
    } 

    public static double round(double d, int decimalPlace){ 
    // see the Javadoc about why we use a String in the constructor 
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double) 
    BigDecimal bd = new BigDecimal(Double.toString(d)); 
    bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP); 
    return bd.doubleValue(); 
    } 
} 
-1

你真的需要發佈一個更完整的例子,所以我們可以看到你想要做什麼。從你發佈的內容來看,這裏是我能看到的。首先,沒有內置的round()方法。您需要撥打Math.round(n)或靜態導入Math.round,然後像您一樣撥打電話。

+0

java中沒有全局函數,只是開始。 – Danon 2017-03-04 10:27:19

12

四捨五入到 「最近」 整數這樣的:

1.4 - >

1.6 - >

-2.1 - >-2

-1.3 - >-1

-1.5 - >- 2

private int round(double d){ 
    double dAbs = Math.abs(d); 
    int i = (int) dAbs; 
    double result = dAbs - (double) i; 
    if(result<0.5){ 
     return d<0 ? -i : i;    
    }else{ 
     return d<0 ? -(i+1) : i+1;   
    } 
} 

您可以根據需要更改條件(結果< 0.5)

+0

我同意anivaler的回答。但是,如果您需要將四捨五入爲最大整數,您可以像下面這樣: double decimalNumber = 4.56777; System.out.println(new Float(Math.round(decimalNumber))); 輸出:5 – Smeet 2015-11-19 11:01:39

24

如果你不喜歡Math.round()你可以使用這個簡單的方法,以及:

int a = (int) (doubleVar + 0.5); 
+1

顯然沒有不喜歡'Math.round()'的有價值的原因:http://stackoverflow.com/a/6468757/1715716 – 2016-02-09 22:02:48

+3

該解決方案更短,不需要導入並且便攜到許多其他編程語言,只需很少的更改。 它可能會更快取決於您的平臺:https://stackoverflow.com/questions/12091014/what-is-the-most-efficient-way-to-round-a-float-value-to-the -nearest-integer-in/12091343#12091343 – 2016-02-11 13:50:27

+0

我不是批評你的答案,我發現**非常有用**因爲你提到的原因。通過這個評論,我解決了那些不敢使用'Math.round()'的人,這個人和你提供的解決方案一樣「字面上」,就是這樣。乾杯。 – 2016-02-11 16:37:25

1

Math.round文檔說:

返回四捨五入的參數結果一個整數。結果 相當於(int) Math.floor(f+0.5)

無需投射到int。也許它是從過去改變過來的。當它接收到一個浮點值

+3

有2個Math.round方法。採用浮點數返回一個整數,這是正確的。但需要雙倍回報的人才會返回很長時間。 – 2015-04-24 21:19:46

0
public static int round(double d) { 
    if (d > 0) { 
     return (int) (d + 0.5); 
    } else { 
     return (int) (d - 0.5); 
    } 
} 
0

的Math.round函數被重載 ,它會給你一個int。例如,這將工作。

int a=Math.round(1.7f); 

當它收到一個double值時,它會給你一個很長的時間,因此你必須將它轉換爲int類型。

int a=(int)Math.round(1.7); 

這樣做是爲了防止精度損失。你的double值是64位,但是你的int變量只能存儲32位,所以它只是把它轉換爲64位的long,但你可以如上所述將它轉換爲32位。