处理数字的类 —— Math类 、 Random类 、 BigDecimal类 与 BigInteger类

在我们学习C语言时,我们处理数据时要调用很多函数,那么,Java也有很多的方法可以来处理数值的类。
那么,在本篇博文中,本人就来讲解三个用于处理数值的类 —— Math类 、 Random类 与 BigDecimal类。

Math类:

概述
Math 类包含用于执行基本数学运算的方法。如初等指数、对数、平方根和三角函数。

在这个类中,有两个很常用的成员 —— E(自然底数)和 PI(圆周率):

public static final double E : 自然底数
public static final double PI: 圆周率

下面时这个类中的常用API

  • public static int abs(int a)
    取绝对值
  • public static double ceil(double a)
    向上取整
  • public static double floor(double a)
    向下取整
  • public static int max(int a,int b)
    获取最大值
  • public static int min(int a, int b)
    获取最小值
  • public static double pow(double a,double b)
    获取a的b次幂
  • public static int round(float a)
    四舍五入
  • public static double sqrt(double a)
    获取正平方根
  • public static double random()
    获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。

看到最后一条API的同学可能会产生疑问——有没有比较方便的产生随机数的手段呢?
答曰:有的。

下面本人就来介绍下专门用于处理随机数的类——Random类

Random类:

概述
此类用于产生随机数
如果用相同的种子创建两个 Random 实例,
则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列

首先是这个类的构造方法

  • public Random()
    没有给定种子,使用的是默认的(当前系统的毫秒值)
  • public Random(long seed)
    给定一个long类型的种子,给定以后每一次生成的随机数是相同的

下面是Random的常用API

  • public int nextInt()
    没有参数 表示的随机数范围 (是int类型随机数的范围)
  • public int nextInt(int n)
    可以指定一个随机数范围
  • void nextBytes(byte[] bytes)
    生成随机字节并将其置于用户提供的空的 byte 数组中。

BigDecimal类:

概述
由于在运算的时候,float类型和double很容易丢失精度
所以,为了能精确的表示、计算浮点数
Java提供了BigDecimal 不可变的、任意精度有符号十进制数

首先本人来介绍下这个类的构造方法

构造方法

  • public BigDecimal(String val)

接下来,本人来介绍一下该类的常用API

  • public BigDecimal add(BigDecimal augend)
  • public BigDecimal subtract(BigDecimal subtrahend)
  • public BigDecimal multiply(BigDecimal multiplicand)
  • public BigDecimal divide(BigDecimal divisor)
    除法
  • public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
    scale 规定 小数点后面保留几位,roundingMode 为 取舍模式 比如四舍五入

BigInteger类:

概述: 不可变的任意精度的整数。
所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)

本人来展示下这个类的常用API

  • BigInteger abs()
    返回其值是此 BigInteger 的绝对值的 BigInteger。
  • BigInteger add(BigInteger val)
    返回其值为 (this + val) 的 BigInteger。
  • BigInteger and(BigInteger val)
    返回其值为 (this & val) 的 BigInteger。
  • BigInteger andNot(BigInteger val)
    返回其值为 (this & ~val) 的 BigInteger。
  • int bitCount()
    返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。
  • int bitLength()
    返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。
  • BigInteger clearBit(int n)
    返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。
  • int compareTo(BigInteger val)
    将此 BigInteger 与指定的 BigInteger 进行比较。
  • BigInteger divide(BigInteger val)
    返回其值为 (this / val) 的 BigInteger。
  • BigInteger[] divideAndRemainder(BigInteger val)
    返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
  • double doubleValue()
    将此 BigInteger 转换为 double。
  • boolean equals(Object x)
    比较此 BigInteger 与指定的 Object 的相等性。
  • BigInteger flipBit(int n)
    返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。
  • float floatValue()
    将此 BigInteger 转换为 float。
  • BigInteger gcd(BigInteger val)
    返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。
  • int getLowestSetBit()
    返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中最右端 1 比特之间的 0 比特的位数)。
  • int hashCode()
    返回此 BigInteger 的哈希码。
  • int intValue()
    将此 BigInteger 转换为 int。
  • boolean isProbablePrime(int certainty)
    如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。
  • long longValue()
    将此 BigInteger 转换为 long。
  • BigInteger max(BigInteger val)
    返回此 BigInteger 和 val 的最大值。
  • BigInteger min(BigInteger val)
    返回此 BigInteger 和 val 的最小值。
  • BigInteger mod(BigInteger m)
    返回其值为 (this mod m) 的 BigInteger。
  • BigInteger modInverse(BigInteger m)
    返回其值为 (this-1 mod m) 的 BigInteger。
  • BigInteger modPow(BigInteger exponent, BigInteger m)
    返回其值为 (thisexponent mod m) 的 BigInteger。
  • BigInteger multiply(BigInteger val)
    返回其值为 (this * val) 的 BigInteger。
  • BigInteger negate()
    返回其值是 (-this) 的 BigInteger。
  • BigInteger nextProbablePrime()
    返回大于此 BigInteger 的可能为素数的第一个整数。
  • BigInteger not()
    返回其值为 (~this) 的 BigInteger。
  • BigInteger or(BigInteger val)
    返回其值为 (this | val) 的 BigInteger。
  • BigInteger pow(int exponent)
    返回其值为 (thisexponent) 的 BigInteger。
  • static BigInteger probablePrime(int bitLength, Random rnd)
    返回有可能是素数的、具有指定长度的正 BigInteger。
  • BigInteger remainder(BigInteger val)
    返回其值为 (this % val) 的 BigInteger。
  • BigInteger setBit(int n)
    返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。
  • BigInteger shiftLeft(int n)
    返回其值为 (this << n) 的 BigInteger。
  • BigInteger shiftRight(int n)
    返回其值为 (this >> n) 的 BigInteger。
  • int signum()
    返回此 BigInteger 的正负号函数。
  • BigInteger subtract(BigInteger val)
    返回其值为 (this - val) 的 BigInteger。
  • boolean testBit(int n)
    当且仅当设置了指定的位时,返回 true。
  • byte[] toByteArray()
    返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。
  • String toString()
    返回此 BigInteger 的十进制字符串表示形式。
  • String toString(int radix)
    返回此 BigInteger 的给定基数的字符串表示形式。
  • static BigInteger valueOf(long val)
    返回其值等于指定 long 的值的 BigInteger。
  • BigInteger xor(BigInteger val)
    返回其值为 (this ^ val) 的 BigInteger。

那么,本篇博文的全部内容就讲解完毕了。
若是对上述知识点或代码有任何疑惑、意见或者建议,请在下方评论区提出,本人将尽早予以答复,觉得有所帮助的同学请留下小赞赞,谢谢!!!

发布了118 篇原创文章 · 获赞 82 · 访问量 5222

猜你喜欢

转载自blog.csdn.net/weixin_45238600/article/details/103972817