我怎麼總是將一個double加到int上,並且永遠不會舍入它。 我知道Math.round(double)
,但我希望它總是四捨五入。 因此,如果它是3.2
,它會四捨五入爲4.總是向上舍入一個雙倍
這可能嗎?
我怎麼總是將一個double加到int上,並且永遠不會舍入它。 我知道Math.round(double)
,但我希望它總是四捨五入。 因此,如果它是3.2
,它會四捨五入爲4.總是向上舍入一個雙倍
這可能嗎?
您可以使用Math.ceil()
方法。
見JavaDoc的鏈接:https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#ceil-double-
從文檔:
小區
public static double ceil(double a)
返回最小的(最接近負無窮大)雙值,該值大於或等於所述參數並且等於一個數學整數。特殊情況:
請注意,Math.ceil(x)的值恰恰是-Math.floor(-x)的值。
參數:
返回:
最小(最接近負無窮大)浮點值,該值大於或等於該參數,並等於某個整數。
如果您可以從鏈接引用,它可以幫助人們在Stack Overflow上找到完整的答案。 –
@ZacharyVance done – rpax
_ceil_返回一個_double_,他要求輸入_int_ –
Math.ceil
公共靜態雙小區會給你最接近的最低值(雙A)
返回最小(最接近負無窮大)雙值大於或等於參數且等於數學整數。特殊情況:
private int roundUP(double d){
double dAbs = Math.abs(d);
int i = (int) dAbs;
double result = dAbs - (double) i;
if(result==0.0){
return (int) d;
}else{
return (int) d<0 ? -(i+1) : i+1;
}
}
幹得好!)
在簡單的話,
Math.ceil
將總是圓形UP或如上面所說的,在過量。Math.round
將根據小數進行舍入或舍入。
Math.ceil
和Math.round
小數< 5.(1,45 = 1)實例:
下面的代碼將返回:
成本,而不的Ceil 2.2和Ceil 3(int),3.0(double)。如果我們圓它:2
int m2 = 2200;
double rate = 1000.0;
int costceil = (int)Math.ceil(m2/rate);
double costdouble = m2/rate;
double costdoubleceil = Math.ceil(m2/rate);
int costrounded = (int)Math.round(m2/rate);
System.out.println("Cost, without Ceil "+costdouble+" and with Ceil "+
costceil+"(int), "+costdoubleceil+"(double). If we round it: "+costrounded);
如果我們平方米的值更改爲例如,其結果將是: 成本,而不的Ceil 2.499並用的Ceil 3(INT),3.0(雙) 。如果我們圓它:2
如果我們平方米的值更改爲例如,其結果將是:
成本,而不的Ceil 2.55並用的Ceil 3(INT),3.0(雙)。如果我們圍繞它:3
希望它有幫助。 (從以前的答案中提取的信息,我只是想說清楚)。
我的方法比較簡單,希望它適合你。
在我的情況下,我有一行對象只能容納3個項目,我必須調整我必須容納這些項目的行數。
所以我有一些Double numberOfRows,然後我使用numberOfRows.intValue()來獲取numberOfRows的int值。
如果我得到的int值小於numberOfRows,我給numberOfRows加1來四捨五入,否則我從numberOfRows.intValue()得到的值就是我想要的答案。
我寫了這個簡單的for循環來測試一下:不使用Math.ceil()
for(int numberOfItems = 0; numberOfItems < 16; numberOfItems++) {
Double numberOfRows = numberOfItems/3.0;
System.out.println("Number of rows are: " + numberOfRows);
System.out.println("Number of items are: " + numberOfItems);
if(numberOfRows.intValue() < numberOfRows) {
System.out.println("int number of rows are: " + (numberOfRows.intValue() + 1));
}
else {
System.out.println("int value of rows are: " + numberOfRows.intValue());
}
System.out.println();
System.out.println();
}
簡短的例子。
public double roundUp(double d){
return d > (int)d ? (int)d + 1 : d;
}
Exaplanation: 比較操作使用類型轉換,如果更大的回報捨去參數+ 1(手段四捨五入)其他不變,操作數四捨五入操作。在僞
double x = 3.01
int roundDown = (int)x // roundDown = 3
if(x > roundDown) // 3.01 > 3
return roundDown + 1 // return 3.0 + 1.0 = 4.0
else
return x // x equals roundDown
例反正你應該使用Math.ceil()
。這只是一個簡單的例子,你可以自己做。
BigDecimal("3.2").setScale(0 , RoundingMode.CEILING)
BigDecimal
如果你想accuracy rather than performance,避免floating point技術。這意味着要避免float
,Float
,double
,Double
。爲了準確,請使用BigDecimal
類。
在BigDecimal
,set the scale上,小數點右邊的位數。如果您不想要小數部分,請將比例設置爲零。並指定一個rounding mode。要總是向上舍入一小部分,請使用RoundingMode.CEILING
,記錄爲:
舍入模式以向正無窮大舍入。如果結果是肯定的,則表現爲RoundingMode.UP;如果爲負值,則表現爲RoundingMode.DOWN。請注意,該舍入模式決不會減少計算值。因此,例如,1.1變爲2,和你的3.2變爲4
BigDecimal bd = new BigDecimal("3.2") ;
BigDecimal bdRounded = bd.setScale(0 , RoundingMode.CEILING) ;
String output = bdRounded.toString() ;
System.out.println("bdRounded.toString(): " + bdRounded) ; // 4
'Math.ceil' - 如果將來有這樣的問題,您可以閱讀相關庫的生成JavaDoc。在這種情況下,Math –
即使你不喜歡RTFM,Math.round(yourNum + 0.5)也可以工作。 – nhgrif
@nhgrif不像'0.0'將四捨五入到'1',這與四捨五入和加1相同。 –