2011-04-25 99 views
20

我想將數字1732四捨五入爲最接近的十個,一百個。我嘗試使用數學回合函數,但它只寫入float和double。如何爲Integer做到這一點? java中有沒有函數?如何在java中整數整數

+0

您選擇的答案被編輯。 – lschin 2011-04-26 03:27:12

回答

11

使用Precision(Apache的百科全書數學3.1.1)

Precision.round(double, scale); // return double 
Precision.round(float, scale); // return float 

使用MathUtils(Apache的百科全書數學) - 舊版本

MathUtils.round(double, scale); // return double 
MathUtils.round(float, scale); // return float 

規模 - 小數點右側的位數。 (+/-)


丟棄,因爲方法輪(浮動, 規模)使用。

Math.round(MathUtils.round(1732,-1)); //最接近10,1730
Math.round(MathUtils.round(1732,-2)); //數百,1700
Math.round(MathUtils.round(1732,-3)); //千位數2000


更好的解決方案

int i = 1732; 
MathUtils.round((double) i, -1); // nearest ten, 1730.0 
MathUtils.round((double) i, -2); // nearest hundred, 1700.0 
MathUtils.round((double) i, -3); // nearest thousand, 2000.0 
+0

我怎麼能做到最接近的12? – 2016-10-04 17:28:16

4

你可以嘗試:

int y = 1732; 
int x = y - y % 10; 

其結果將是1730

編輯:此不回答這個問題。它只是刪除了部分數字,但並沒有「到最近的」。

+4

1736也會到1730年,這不會是最近的十年。 – EboMike 2011-04-25 06:35:33

+1

...... 1739的結果也將是1730.不是大多數人對舍入的期望。 – rlibby 2011-04-25 06:36:04

+0

@EboMike:你是100%的權利。 – cherouvim 2011-04-25 06:36:08

22

你想使用什麼舍入機制?這裏有一個原始的方式,爲正數:

int roundedNumber = (number + 500)/1000 * 1000; 

這將帶來類似1499年至1000年和1500至2000年

如果你能有負數:

int offset = (number >= 0) ? 500 : -500; 
int roundedNumber = (number + offset)/1000 * 1000; 
+1

匿名downvoter,謝謝。任何原因? – EboMike 2011-04-25 07:21:03

+1

這裏是正確的方法:int r =((int)((n + 500)/ 1000))* 1000因爲只有鑄造到int切斷數字 – 2012-07-11 09:24:18

+5

@nils:這沒有任何意義。這些數字已經是整數(按照OP),所以將一個int值轉換爲int值什麼也不做。 – EboMike 2012-07-11 16:21:47

2

爲什麼不檢查單位數字... 1.如果小於或等於5,則在單元位置加0,並保持原來的數字。 2.如果大於5,則增加十位數,在單元位置加0。

例如:1736(自6> = 5)的倒圓的數目將現在1740 爲1432(因爲2 < 5)的圓形數目將是1430 ....

希望這將工作......如果不是讓我知道的情況下...

快樂編程,

11
(int)(Math.round(1732/10.0) * 10) 

Math.round(double)採取double,然後爲最接近的整數四捨五入。因此,1732將變爲173.2(輸入參數)處理Math.round(1732/10.0)。所以方法就像173.0一樣。然後將它乘以 (Math.round(1732/10.0) * 10)給出了舍入的答案,即173.0將被輸出到int

+1

我認爲10D是你的意思,而不是10L。不是嗎? +1 – 2011-04-25 06:43:52

+0

好吧,它的工作原理,但你最好解釋你的答案是如何工作的 – 2015-05-26 11:52:51

+0

@MuhammedRefaat,'Math.round(double)'取得double,然後以整數向上舍入。因此,'Math.round()'處理時'1732'將變爲'173.2'。所以這個方法就像'173.0'一樣。然後乘以10得出舍入答案。 – Sibidharan 2016-01-20 21:10:30

1

很簡單。試試這個

int y = 173256457;int x = (y/10)*10; 

在現在這個可以通過100,1000代替10等等....

+0

正如其他答案所述,這將帶來1739年至1730年,而不是1740年。 – EboMike 2011-04-25 07:21:32

1

它很容易..

INT X = 1234; int y = x - x%10; //它會給出1230

int y = x - x%100; //它將給出1200

int y = x - x%1000; //它會給出1000

上面的邏輯只會將最後的數字轉換爲0.如果您想實際一輪// 例如。1278這應該四捨五入到1280,因爲最後一位數字8> 5爲此我寫了一個函數檢查出來。

private double returnAfterRoundDigitNum(double paramNumber, int noOfDigit) 
    { 
    double tempSubtractNum = paramNumber%(10*noOfDigit); 
    double tempResultNum = (paramNumber - tempSubtractNum); 
    if(tempSubtractNum >= (5*noOfDigit)) 
     { 
      tempResultNum = tempResultNum + (10*noOfDigit); 
     } 
     return tempResultNum; 
    } 

這裏傳遞2個參數一個是數量,另一個是直到你必須四捨五入位置。

問候, 阿比納夫

4

在最近十:

int i = 1986; 
int result; 

result = i%10 > 5 ? ((i/10)*10)+10 : (i/10)*10; 

(隨意添加零對一百萬元)。

+0

這會舍入下面的數字(四捨五入到最接近的100),應該向下舍入:170718被舍入到170800.當然,這應該舍入到170700? – Adam893 2015-07-06 08:51:37

0

你看過Mathutils.round()的實現嗎?它都基於BigDecimal和字符串轉換。很難想象很多效率較低的方法。

0

不使用任何數學utils的,四捨五入可以實現到如下任何單位:

double roundValue (double input, double toNearest){ 
//toNearest is any rounding base like 10, 100 or 1000. 
    double modValue = input % toNearest; 
    System.out.println(modValue); 
    if(modValue == 0d){ 
     roundedValue = input; 
    } 
    else 
    { 
     roundedValue = ((input - modValue) + toNearest); 
    } 
    System.out.println(roundedValue); 
    return roundedValue; 
} 
0

我做這個簡單的方法,它爲我工作得很好。它四捨五入到最接近的十位,但很少大慶石油管理局可以圓100,1000等

public int round10(int num){ 
    if (num%10 < 5){ 
     while (num%10!=0){ 
      num--; 
     } 
    } 
    else { 
     while (num%10!=0){ 
      num++; 
     } 
    } 
    return num; 
} 
0

我通常做這種方式:

int num = 1732; 
int roundedNum = Math.round((num + 9)/10 * 10); 

這會給你1740作爲結果。

希望這會有所幫助。