[Java]除法之無條件進位
Source File Name : RoundUp.java
-----------------------------------------------------------
import java.math.*;
class RoundUp
{
public static void main(String[] args)
{
int a1 = 10;
int a2 = 3;
int intresult;
String strresult;
BigDecimal b1 = new BigDecimal(String.valueOf(a1));
BigDecimal b2 = new BigDecimal(String.valueOf(a2));
//將 BigDecimal 轉成 int
intresult = ((b1.divide(b2,0,BigDecimal.ROUND_UP)).toBigInteger()).intValue();
//將 BigDecimal 轉成 String
strresult = (b1.divide(b2,0,BigDecimal.ROUND_UP)).toString();
System.out.println( String.valueOf(a1) + "÷" + String.valueOf(a2)
+ " 無條件進位 " + strresult );
//將 int 轉成 String
System.out.println( String.valueOf(a1) + "÷" + String.valueOf(a2)
+ " 無條件進位 " + String.valueOf(intresult) );
}
}
-----------------------------------------------------------
Result :
-----------------------------------------------------------
10÷3 無條件進位 4
10÷3 無條件進位 4
-----------------------------------------------------------