在我们学习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类的更多相关文章

  1. [常用类]Math、Random、System、BigInteger、BigDecimal

    Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象 常用方法: int abs() double ceil() //向上取整 double floor() //向下取整 int ma ...

  2. Java基础 【Math、Random、System、BigInteger、BigDecimal、Date、Calendar等常用类的使用】

    学习的这几个类  是日常工作中经常要使用到的类 Math 类包含用于执行基本数序运算的方法,如初等指数.对数.平方根和 三角函数. 成员方法 1.public static int abs(int a ...

  3. Math、Random、System、BigInteger、Date、DateFormat、Calendar类,正则表达式_DAY14

    1:Math&大数据类四则运算 X abs(X x) double random()         产生随机数 double ceil(double a)   向上取整 double flo ...

  4. 3.4常用类(java学习笔记)Math和Random

    一.Math 这个类包含执行指数.对数.平方根法.三角函数等基本的数字运算. Math中还包含一些静态常量供我们调用. 如PI圆周率,E. 1.abs(),返回该数的绝对值. public class ...

  5. 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)

    1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则)     * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ...

  6. Date日期类,Canlendar日历类,Math类,Random随机数学类

    Date日期类,SimpleDateFormat日期格式类 Date  表示特定的时间,精确到毫秒 常用方法 getTime() setTime() before() after() compareT ...

  7. Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类

    Math类* A:Math类概述 * Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. * B:成员方法 * public static int abs(int a)  ...

  8. Java中正则表达式及其常用类Math、Calendar、Date、BigDecimal、BigInterger、System、Rondom的使用

    1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ( ...

  9. Java学习笔记26(Math类、Arrays类、BigInteger类、BigDecimal类)

    Math类:数学工具类,做一些数学计算,开方,对数,三角函数等 所有方法都是静态方法,不需要建立对象,直接用类名调用即可 示例: 这里写几个在日常开发中会用到的,比如三角函数之类的平时不会用到,了解即 ...

随机推荐

  1. 【笔记3-31】Python语言基础-字典dict

    创建字典 dict1 = {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'} dict2 = dict(k1='v1', k2='v2', k3='v3' ...

  2. Java集合框架要点概括(Core Knowledge of Java Collection)

    目录 有哪些集合类 Set类 Queue类 List类 Map类 HashMap的实现原理,是否线程安全,如何使其做到线程安全 HashMap的实现原理 HashMap的数据结构 HashMap的存取 ...

  3. OpenCV-Python 霍夫圈变换 | 三十三

    学习目标 在本章中, 我们将学习使用霍夫变换来查找图像中的圆. 我们将看到以下函数:cv.HoughCircles() 理论 圆在数学上表示为(x−xcenter)2(y−ycenter)2=r2(x ...

  4. coding++:java操作 FastDFS(上传 | 下载 | 删除)

    开发工具  IDEAL2017  Springboot 1.5.21.RELEASE --------------------------------------------------------- ...

  5. Spring事物传播行为

    Spring事物传播行为 Spring中事务的定义: Propagation(key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为.)有以下选项可供使用: PROPAGATI ...

  6. 小案例带你揭秘JS事件

    小案例带你揭秘JS事件 ### 什么是事件? 在js中一个事件的组成由那些呢? 谁触发事件:事件源 触发什么事件: 事件的类型 触发事件干什么事:事件处理函数 事件传播的过程 捕获阶段 就是从wind ...

  7. MyBatis 教程 ——检视阅读

    MyBatis 教程 --检视阅读 准备 官网文档-中文 教程地址yiibai,质量很差 教程地址w3cschool,纯理论,还不如直接看官网文档 教程地址Mybatis框架入门教程,Oracle M ...

  8. Linux - Ubuntu18.04下更改apt源为阿里云源

    进入apt目录,备份原来的源地址 cd /etc/apt mv ./source.list ./source.list.bak 修改源文件source.list vim source.list 更换阿 ...

  9. JAVA WEB随笔

    servlet生命周期: 1.构造器(单利)2.init(单例)3.service(多例,每次请求都会执行)4.销毁方法destroyservlet解析客户端请求流程:1.web客户端向servlet ...

  10. Redis对象——列表(List)

    列表(list)类型是用来存储多个有序的字符串,列表中的每个字符串称为元素(element),一个列表最多可以存储232-1个元素.在Redis中,可以对列表两端插入(push)和弹出(pop),还可 ...