java.math.BigDecimal
作用
BigDecimal实例表示不可变的、任意精度的、符号的十进制数
BigDecimal实例 由任意精度的整数非标度值和32位的整数标度(scale)组成
常量
public static final BigDecimal ZERO(值为0,标度为0)
public static final BigDecimal ONE(值为1,标度为0)
public static final BigDecimal TEN(值为10,标度为0)
构造方法
public BigDecimal(int|long|double value)
public BigDecimal(int| long| double value,MathContext mc)
public BigDecimal(String value)
public BigDecimal(String value,MathContext mc)
…
类方法
public static BigDecimal valueof(long value)
public static BigDecimal valueof(double value)
public static BigDecimal valueof(long unscaledValue ,int scale)
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigDecimalTest2 {
public static void main(String[] args) {
**BigDecimal** a = **BigDecimal**.valueOf( 100200300 ); // ( long )<br /> **System**.out.println( a );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~" );
**BigDecimal** b = **BigDecimal**.valueOf( **3.14** ); // ( double )<br /> **System**.out.println( b );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~" );
**BigDecimal** c = **BigDecimal**.valueOf( **3.14159265358979323846** ); // ( double )<br /> **System**.out.println( c );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~" );
**final** **long** uv = 3141592653589793238L ;<br /> **final** **int** s = 18 ;<br /> **BigDecimal** d = **BigDecimal**.valueOf( uv , s ); // ( long , int )<br /> **System**.out.println( d );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~" );
**final** **String** value = "3141592653589793238462643383279";<br /> // 采用 BigInteger 实例所表示的不可变的任意精度的整数 来充当 非标度值 ( unscaledValue )<br /> **final** **BigInteger** unscaledValue = **new** **BigInteger**( value );<br /> // 根据实际需要来指定 标度 ( scale ) 值<br /> **final** **int** scale = value.length() - 1 ; // 因为为圆周率的小数点之前只有一位数字
// 根据 非标度值 和 标度值 来创建一个 BigDecimal 实例,用来表示 不可变的、任意进度的十进制数<br /> **BigDecimal** f = **new** **BigDecimal**( unscaledValue , scale ); // ( BigInteger , int )<br /> **System**.out.println( f );<br /> }<br />}
实例方法
public BigDecimal divide(BigDecimal divisor)//除法
public BigDecimal divide(BigDecimal divisor,int scale,MathContext mc)
public BigDecimal divide(BigDecimal divisor,MathContext)
public BigDecimal negate(MathContext mc)//取相反数,但是地址不同。
unscaledValue = unscaledValue.negate();
public BigDecimal round(MathContext mc)
MathContext mc = new MathContext( 5 , HALF_UP ); // 使用 “静态导入“ __后,书写简单了,但可读性变差了_BigDecimal w = x.round( mc ); // 将 x 所表示的 十进制数 按照指定的 MathContext _进行取
public BigDecimal movePointLeft(int n)//将小数点向左移动n位
public BigDecimal movePointright(int n)
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class BigDecimalTest5 {
public static void main(String[] args) {
**BigDecimal** x = **BigDecimal**.valueOf( 12 , 1 ); // ( 1212 , 1 ) 、( 12 , 2 )<br /> **System**.out.println( x );
**BigDecimal** y = **BigDecimal**.valueOf( 37 , 1 ) ;<br /> **System**.out.println( y );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
**System**.out.println( x + " + " + y + " = " + x.add( y ) );<br /> **System**.out.println( x + " - " + y + " = " + x.subtract( y ) );<br /> **System**.out.println( x + " * " + y + " = " + x.multiply( y ) );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
// System.out.println( x + " / " + y + " = " + x.divide( y ) ); // ArithmeticException
**MathContext** mc = **new** **MathContext**( 5 , **RoundingMode**.HALF_UP ) ;<br /> // public BigDecimal divide( BigDecimal divisor , MathContext mc )<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , mc ) );
mc = **new** **MathContext**( 3 , **RoundingMode**.HALF_UP ) ;<br /> // public BigDecimal divide( BigDecimal divisor , MathContext mc )<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , mc ) );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
// public BigDecimal divide( BigDecimal divisor , int scale(**有效数字**) , RoundingMode mode(舍入方法) )<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , 4 , **RoundingMode**.HALF_UP ) );<br />//1.2 / 3.7 = 0.3243<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , 3 , **RoundingMode**.HALF_UP ) );<br />//1.2 / 3.7 = 0.324<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , 2 , **RoundingMode**.HALF_UP ) );<br />//1.2 / 3.7 = 0.32<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , 1 , **RoundingMode**.HALF_UP ) );<br />//1.2 / 3.7 = 0.3<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , 0 , **RoundingMode**.HALF_UP ) );<br />//1.2 / 3.7 = 0<br /> **System**.out.println( x + " / " + y + " = " + x.divide( y , -1 , **RoundingMode**.HALF_UP ) );<br />//1.2 / 3.7 = 0E+1<br /> }<br />}
父类
java.lang.Nunmber
BigInteger
作用
构造方法
String value = “8fa3”;int radix = 16 ;
public static BigInteger( value , radix );
public static**BigInteger( byte[] )**
类方法
public static BigInteger valueOf (long value)
public static BigInteger probablePrime(int bitLength ,Rondom r)
实例方法
public BigInteger abs()
public BigInteger add(BigInteger value)
public BigInteger subtract(BigInteger value)
public BigInteger multiply(BigInteger value)
public BigInteger divide(BigInteger value)
public BigInteger max(BigInteger val) //返回两个大整数的最大者
public BigInteger min(BigInteger val) //返回两个大整数的最小者
public BigInteger mod(BigInteger val) //用当前大整数对val求模
public BigInteger pow(int exponent)
public BigInteger and(BigInteger value)
public BigInteger or(BigInteger value)//返回两个大整数的按位或
public BigInteger xor(BigInteger value)//返回两个大整数的异或
public BigInteger not()//BigInteger a = x.not(); //返回其值为 (~this) 的 BigInteger
public BigInteger shiftLeft(int n)
public BigInteger shiftRight(int n)
public byte[] toByteArray()
public BigInteger remainder(BigInteger val) //返回当前大整数除以val的余数
import java.math.BigInteger;
/
*/
public class BigIntegerTest3 {
public static void main(String**[] args) {
**final** **byte**[] bytes = { -1 , 99 };
**BigInteger** x = **new** **BigInteger**( "9527" ); // 按照十进制解析字符串<br /> **System**.out.println( x );
**BigInteger** y = **new** **BigInteger**( bytes );<br /> **System**.out.println( y );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
**BigInteger** z = x.add( y );
**System**.out.println( "z = " + z );<br /> **System**.out.println( "x = " + x );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
**BigInteger** a = x.not(); //返回其值为 (~this) 的 BigInteger<br /> **System**.out.println( "a = " + a );<br /> **System**.out.println( "x = " + x );
**BigInteger** b = x.negate();<br /> **System**.out.println( "b = " + b );//返回其值为 (-this) 的 BigInteger<br /> **System**.out.println( "x = " + x );
**System**.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
BigInteger c = x.shiftLeft( 1 ); // x << 1
**System**.out.println( "c = " + c + " , x = " + x );
**BigInteger** d = y.shiftLeft( 1 ); // y >> 1<br /> **System**.out.println( "d = " + d + " , y = " + y );<br /> }<br />}
父类
java.lang.Number
java.math.MathContext
作用
MathContext 实例用来封装 上下文设置的不可变对象
通过上下文来设置描述 数字运算符的某些规则
设计
precision 精度
roundingMode (舍入模式)
构造方法
public MathContext(int precision)
public MathContext(int precision,RoundingMode roundingMode)
public MathContext(String value)
实例方法
public int getPrecision()
public RoundingMode getRoundingMode()