处理数字的类 —— 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。
处理数字的类 —— Math类 、 Random类 、 BigDecimal类 与 BigInteger类的更多相关文章
- [常用类]Math、Random、System、BigInteger、BigDecimal
Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象 常用方法: int abs() double ceil() //向上取整 double floor() //向下取整 int ma ...
- Java基础 【Math、Random、System、BigInteger、BigDecimal、Date、Calendar等常用类的使用】
学习的这几个类 是日常工作中经常要使用到的类 Math 类包含用于执行基本数序运算的方法,如初等指数.对数.平方根和 三角函数. 成员方法 1.public static int abs(int a ...
- Math、Random、System、BigInteger、Date、DateFormat、Calendar类,正则表达式_DAY14
1:Math&大数据类四则运算 X abs(X x) double random() 产生随机数 double ceil(double a) 向上取整 double flo ...
- 3.4常用类(java学习笔记)Math和Random
一.Math 这个类包含执行指数.对数.平方根法.三角函数等基本的数字运算. Math中还包含一些静态常量供我们调用. 如PI圆周率,E. 1.abs(),返回该数的绝对值. public class ...
- 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)
1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则) * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ...
- Date日期类,Canlendar日历类,Math类,Random随机数学类
Date日期类,SimpleDateFormat日期格式类 Date 表示特定的时间,精确到毫秒 常用方法 getTime() setTime() before() after() compareT ...
- Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类
Math类* A:Math类概述 * Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. * B:成员方法 * public static int abs(int a) ...
- Java中正则表达式及其常用类Math、Calendar、Date、BigDecimal、BigInterger、System、Rondom的使用
1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ( ...
- Java学习笔记26(Math类、Arrays类、BigInteger类、BigDecimal类)
Math类:数学工具类,做一些数学计算,开方,对数,三角函数等 所有方法都是静态方法,不需要建立对象,直接用类名调用即可 示例: 这里写几个在日常开发中会用到的,比如三角函数之类的平时不会用到,了解即 ...
随机推荐
- 采用vue编写的功能强大的swagger-ui页面
think-swagger-ui-vuele swagger-ui有非常多的版本,觉得不太好用,用postman,每个接口都要自己进行录入.所以在基于think-vuele进行了swagger格式js ...
- pycharm创建虚拟环境venv和添加依赖库package
1.创建虚拟环境 因为项目采用不同版本的python,所依赖的库的版本也不一样,为了避免版本冲突,为每一个项目每个python版本创建一个虚拟环境,环境中所使用的依赖库也是独立存在,不会被其他版本或其 ...
- 机器学习3- 一元线性回归+Python实现
目录 1. 线性模型 2. 线性回归 2.1 一元线性回归 3. 一元线性回归的Python实现 3.1 使用 stikit-learn 3.1.1 导入必要模块 3.1.2 使用 Pandas 加载 ...
- OpenCV-Python 图像梯度 | 十八
目标 在本章中,我们将学习: 查找图像梯度,边缘等 我们将看到以下函数:cv.Sobel(),cv.Scharr(),cv.Laplacian()等 理论 OpenCV提供三种类型的梯度滤波器或高通滤 ...
- 线程 -- ThreadLocal
1,ThreadLocal 不是“本地线程”的意思,而是Thread 的局部变量.每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本 2, ...
- sql MariaDB 安装contos
安装和运行MySQL数据库(MariaDB) centos 平台 1.安装和运行 yum install mariadb mariadb-server - 安装 systemctl start mar ...
- [noip模拟]数字对<RMQ&二分>
数字对 [题目描述] 小H是个善于思考的学生,现在她又在思考一个有关序列的问题. 她的面前浮现出一个长度为n的序列{ai},她想找出一段区间[L, R](1 <= L <= R <= ...
- kerberos系列之flink认证配置
大数据安全系列的其它文章 https://www.cnblogs.com/bainianminguo/p/12548076.html-----------安装kerberos https://www. ...
- LayUI制作日历工作记录簿
标题不知道该如何取,大概就是用Lay UI的Table,制作一个日历,在日历上可以添加每天的工作简记录.记录下LayUI Table的一些用法,一些值得探索的地方在于日历生成后,给周末加背景色,当天加 ...
- springboot项目下的Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
今天遇到mybatis-puls的报错Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (n ...