1. 程式人生 > >Java中,BigDecimal互轉Integer

Java中,BigDecimal互轉Integer

一、Integer型別 定義:
Integer a=new Integer(int value); 
Integer a=new Integer(String value);
轉換: 1)定義中就可以將int型和String型的轉換為Integer型 2)String型別轉換為Integer型
Integer.valueOf("");
Integer.getInteger("");
3)String、Integer型別轉換為int型
Integer.parseInt("");
Integer a;
a.intValue();
4)上面定義的Integer a轉換為float, double, long
a.floatValue();
a.doubleValue();
a.longValue();
5)Integer a轉換為String(其它的型別轉換為String都可通用以下方法)
toString();
String.valueOf(a);
6)Integer a轉換為BigDecimal
BigDecimal temp = new BigDecimal(a);
比較(比較的數Integer a): 1)
Int num=a.compareTo(Integer anotherInteger);
如果該 Integer 等於 Integer 引數,則返回 0 值;如果該 Integer 在數字上小於 Integer 引數,則返回小於 0 的值;如果 Integer 在數字上大於 Integer 引數,則返回大於 0 的值(有符號的比較)。 2)轉換為int型再比較 a.intValue()與b.intValue比較大小; 二、BigDecimal
定義:
BigDecimal a=new BigDecimal(String; val)
BigDecimal a=new BigDecimal(double val);
轉換: 1)定義中就可以將String型和double 型的轉換為BigDecimal型 2)Int,float, double, long轉換為BigDecimal
a.floatValue();
a.doubleValue();
a.longValue();
a.intValue();
3) BigDecimal a轉換為String(其它的型別轉換為String都通用以下方法)
toString();
String.valueOf(a);
比較(比較的數BigDecimal a)
1)Int num=a.compareTo(BigDecimal anotherBigDecimal);
當此BigDecimal在數字上小於、等於或大於 val 時,返回 -1、0 或 1。 BigDecimal取其中最大、最小值、絕對值、相反數:
a.max (b) //比較取最大值
a.min(b) //比較取最小值
a.abs()//取最絕對值
a.negate()//取相反數
計算:
加: a.add(b);
減: a.subtract(b);
乘: a.multiply(b);
除: a.divide(b,2);//2為精度取值
int 、long、double、 float的取絕對值和同型別間比較大小都可用以下Math方法:
Math.max(a,b);//比較取最大值
Math.min(a,b);//比較取最小值
Math.abs(a); //取最絕對值