Number,Math与大数值

Number类

Nunber类结构

在这里插入图片描述
所有的包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类。

常用方法

abstract xxx xxxValue()方法
用于将Number类对象转化为xxx数据类型的值并返回。注意,这个方法不接受任何参数,包装类对象调用。

示例:

Float a = 1.0f;
System.out.println(a.intValue());
System.out.println(a.doubleValue());

运行结果:

1
1.0

底层代码实现:

/**
     * Returns the value of this {@code Float} as an {@code int} after
     * a narrowing primitive conversion.
     *
     * @return  the {@code float} value represented by this object
     *          converted to type {@code int}
     * @jls 5.1.3 Narrowing Primitive Conversions
     */
    public int intValue() {
        return (int)value; //直接强制转换
    }

    /**
     * Returns value of this {@code Float} as a {@code long} after a
     * narrowing primitive conversion.
     *
     * @return  the {@code float} value represented by this object
     *          converted to type {@code long}
     * @jls 5.1.3 Narrowing Primitive Conversions
     */
    public long longValue() {
        return (long)value; //直接强制转换
    }
    ...

Math类

常用方法

static double ceil(double a);

返回大于参数a的最小整数

static double floor(double a);

返回小于参数的最大整数

static long round(double a);

返回四舍五入的整数

static double pow(double a, double b);

返回a的b次方值

static double sqrt(double a);

返回a的平方根值

static double abs(double a);

返回a的绝对值。另外还有适合int,long,float的方法

static double max(double a, double b);

返回a,b之间的数值较大者

static double min(double a, double b);

返回a,b之间的数值较小者

static double random();

返回大于等于0.0且小于1.的double值

示例:

package math;

public class MathDemo {
	public static void main(String[] args) {
		//返回大于参数的最小整数
		System.out.println(Math.ceil(15.5));
		//返回小于参数的最大整数
		System.out.println(Math.floor(15.5));
		//返回四舍五入的整数
		System.out.println(Math.round(-15.5));
		System.out.println(Math.round(15.5));
		//返回a的b次方值
		System.out.println(Math.pow(12, 2));
		//返回a的平方根值
		System.out.println(Math.sqrt(4));
		//返回a的绝对值。另外还有适合int,long,float的方法
		System.out.println(Math.abs(-15.5));
		//返回a,b之间的数值较大者
		System.out.println(Math.max(15, 45));
		//返回a,b之间的数值较小者
		System.out.println(Math.min(15, 45));
		//返回大于等于0.0且小于1.的double值
		System.out.println(Math.random());
	}
}

运行结果:

16.0
15.0
-15
16
144.0
2.0
15.5
45
15
0.5322750043361659

大数值

BigInteger类

BigInteger 支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息。

常用方法:
BigInteger(String val);

构造函数,使用字符串表示的值去实例化一个BigInteger对象

static BigInteger valueOf(long val);

返回一个BigInteger,其值等于指定long的值。

BigInteger add(BigInteger other);
BigInteger subtract(BigInteger other);
BigInteger multiply(BigInteger other);
BigInteger divide(BigInteger other);
BigInteger mod(BigInteger other);

返回这个大整数和另一个大整数other的和、差、积、商以及余数。

int compareTo(BigInteger other);

如果这个大整数与另一个大整数other相等,返回0;如果这个大整数小于另一个大整数other,返回-1;否则,返回1。

BigDecimal类

BigDecimal 和 BigInteger 都能用来实现大数字的运算,不同的是 BigDecimal 加入了小数的概念。一般的 float 型和 double 型数据只可以用来做科学计算或工程计算,但由于在商业计算中要求数字精度比较高,所以要用到 java.math.BigDecimal 类。 BigDecimal 类支持任何精度的定点数,可以用它来精确计算货币值

重点方法:
BigDecomal divide(BigDecimal divisor , int sacle ,int roundingMode);

做除法操作,方法中 3 个参数分别代表除数、商的小数点后的位数、近似处理模式

BigDecimal 类中 divide() 方法的多种处理模式:

模式 含义
BigDecimal.ROUND_UP 商的最后一位如果大于 0 ,则向前进位,正负数都如此
BigDecimal .ROUND_DOWN 商的最后一位无论是什么数字都省略
BigDecimal .ROUND_CEILING 商如果是正数,按照 ROUND_UP 模式处理;如果是负数,按照 ROUND_DOWN 模式处理。这种模式的处理都会使近似值大于等于实际值。
BigDecimal .ROUND_FLOOR 与 ROUND_CEILING 模式相反,商如果是正数,按照 ROUND_DOWN 模式处理;如果是负数,按照ROUND_UP 模式处理。这种模式的处理都会使近似值小于等于实际值。
BigDecimal .ROUND_HALF_DOWN 对商进行四舍五入操作,如果商最后一位小于等于 5,则做舍弃操作;如果最后一位大于 5 ,则做进位操作,如 7.5 ≈ 7
BigDecimal ROUND_HALF_EVEN 如果商的倒数第二位为奇数,则按照 ROUND_HALF_UP 处理;如果为偶数,则按照 ROUND_HALF_DOWN 处理,如 7.5 ≈ 8 , 8.5 ≈ 8
BigDecimal .ROUND_HALF_UP 对商进行四舍五入操作,如果商的最后一位小于 5 则舍弃;如果大于等于 5 ,进行进位操作,如 7.5 ≈ 8

代码示例(使用BigDecimal .ROUND_HALF_UP处理模式):

private static void testBigDecimalDicide() {
	BigDecimal num = new BigDecimal(16.0);
	BigDecimal other = new BigDecimal(5.1);
	//进行除法运算
	BigDecimal result = num.divide(other, 10, BigDecimal.ROUND_HALF_UP);
	System.out.println(result);
}

运行结果:

3.1372549020

参看文章:https://blog.csdn.net/js940814/article/details/80210456

猜你喜欢

转载自blog.csdn.net/weixin_40374341/article/details/86152867
今日推荐