在我们学习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. [dp+博弈]棋盘的必胜策略

    链接:https://ac.nowcoder.com/acm/problem/21797来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K ...

  2. Trie树的简单实现

    import java.util.ArrayList; import java.util.TreeMap; import util.FileOperation; public class Trie { ...

  3. MySQL到底能有多少个字段

    今天技术讨论群里 “一切随遇而安”同学看书时出现一个疑问,一个MySQL的表中到底可以有多少个字段?带着这个疑问,我们展开了探讨,也接着讨论了一个单字段长度的问题. 1.  官方文档说明 官方文档的内 ...

  4. Jmeter接口测试之用户自定义变量(九)

    在使用Jmeter做接口自动化测试中,经常会使用到公共的数据,那么就需要对这些公共的数据分离出来,不管是基于测试框架的思想,还是使用工具来进行做自动化测试,公共数据的分离首先是需要思考的.这里就以获取 ...

  5. WePY框架 input,checkbox-group,radio-group等change 一般处理方法

    布局搞定了,接下来就是数据处理方面了 form表单中常用标签,绑定change方法: 方法的具体实现 根据打印出来e的结果可以看到,e指代当前标签对象,包含属性方法等 从detail中可以获取多选框选 ...

  6. 移植OPENNI到DM6446上面

    为了利用摄像头的景深信息,同时利用dm6446的分析功能(dsp),对openNI进行USB移植. 下载:libusb-1.0.0.tar.bz2            OpenNI-Stable-1 ...

  7. coding++:win10家庭版升级专业版方案

    win10家庭版升级专业版密钥: VK7JG-NPHTM-C97JM-9MPGT-3V66T 4N7JM-CV98F-WY9XX-9D8CF-369TT FMPND-XFTD4-67FJC-HDR8C ...

  8. Springcloud config + zuul 搭建动态网关

    1,实现的效果,就是zuul 网关的配置路由实现负载均衡,zuul 的配置文件放在springcloud config 上 2,需要的服务如下: 3,其实就是配置下springcloud-zuul 的 ...

  9. Thread wait notify sleep

    wait: 必须暂定当前正在执行的线程,并释放资源锁,让其他线程可以有机会运行 notify/notifyall: 唤醒因锁池中的线程,使之运行 wait与sleep区别 对于sleep()方法,我们 ...

  10. Spring中的设计模式:工厂方法模式

    导读 工厂方法模式是所有设计模式中比较常用的一种模式,但是真正能搞懂用好的少之又少,Spring底层大量的使用该设计模式来进行封装,以致开发者阅读源代码的时候晕头转向. 文章首发于微信公众号[码猿技术 ...