15.Java-正则表达式、Pattern和Matcher类、Math类、Random类、System类、BigDecimal类、BigInteger类

15.Java-正则表达式、Pattern和Matcher类、Math类、Random类、System类、BigDecimal类、BigInteger类

一、正则表达式的概述和简单使用

A:正则表达式:正确规则的表达式 规则java给我们定的
	是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
B:案例演示
	需求:校验qq号码.
		1:要求必须是5-15位数字
		2:0不能开头
		
	a:非正则表达式实现

	b:正则表达式实现 
package org.westos.java17;

import java.util.Scanner;

/*        需求:校验qq号码.
        1:要求必须是5-15位数字
        2:0不能开头

        a:非正则表达式实现

        b:正则表达式实现*/
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        boolean checkqq = checkqq(s);
        System.out.println(checkqq);

    }
    public static boolean checkqq(String s){
        boolean b=false;
        if(s.length()>=5&&s.length()<=15&&!(s.charAt(0)=='0')){
            for (int i = 0; i < s.length(); i++) {
                if(!Character.isDigit(s.charAt(i))){
                    return  false;
                }
            }
            return true;
        }
        return b;
    }
}

二、正则表达式的组成规则

规则字符在java.util.regex Pattern类中
A:字符
	x 字符 x。举例:'a'表示字符a
	\\ 反斜线字符。
	\n 新行(换行)符 ('\u000A') 
	\r 回车符 ('\u000D')
B:字符类
	[abc] a、b 或 c(简单类) 
	[^abc] 任何字符,除了 a、b 或 c(否定) 
	[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
	[0-9] 0到9的字符都包括
C:预定义字符类
	. 任何字符。我的就是.字符本身,怎么表示呢? \.
	\d 数字:[0-9]
	\w 单词字符:[a-zA-Z_0-9]
		在正则表达式里面组成单词的东西必须有这些东西组成
	\s 匹配空格字符	
D:边界匹配器
	^ 行的开头 
	$ 行的结尾 
	\b 单词边界
		就是不是单词字符的地方。
		举例:hello world?haha;xixi
E:Greedy 数量词 
	X? X,一次或一次也没有 比如""空串 就是没有
	X* X,零次或多次  大于等于1次 都算多次
	X+ X,一次或多次
	X{n} X,恰好 n 次 
	X{n,} X,至少 n 次 
	X{n,m} X,至少 n 次,但是不超过 m 次 
package org.westos.java17;

import java.util.Scanner;

/*        需求:校验qq号码.
        1:要求必须是5-15位数字
        2:0不能开头

        a:非正则表达式实现

        b:正则表达式实现*/
public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String regx="[1-9][0-9]{4,14}";
        boolean matches = s.matches(regx);
        System.out.println(matches);
    }
}
package org.westos.java17;

import java.util.Scanner;

//校验手机号的规则
//手机号是 11位 每一位是数字 以1开头 第二位是 3 5 6 7 8 其余位是任意数字
public class Test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        boolean b=checkphonenum(s);
        System.out.println(b);
    }

    private static boolean checkphonenum(String s) {
        boolean b=false;
        if(s.length()==11&&(s.startsWith("13")||s.startsWith("15")||s.startsWith("16")||s.startsWith("17")||s.startsWith("18"))){
            for (int i = 2; i < s.length(); i++) {
                if(!Character.isDigit(s.charAt(i))){
                    b=false;
                    break;
                }else{
                    b=true;
                }
            }
        }
        return b;
    }
}
package org.westos.java17;

import java.util.Scanner;

//校验手机号的规则
//手机号是 11位 每一位是数字 以1开头 第二位是 3 5 6 7 8 其余位是任意数字
public class Test4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String regx="1[35678][0-9]{9}";
        boolean matches = s.matches(regx);
        System.out.println(matches);
    }
}

package org.westos.java17;

public class Test6 {
    public static void main(String[] args) {
        //正则表达式:正确规则的表达式,是一个独立语法,很多语言都会支持正则表达式,
        // 主要作用,就是用来定义一些规则,进而校验数据是否符合正则表达式所描述的规则

        //Java中定义正则表达式可以使用字符串来定义
        // String regx="正则表达式的语法";
        //var regx=/正则表达式语法/ js中定义正则表达式的方式

        String regx = "abc";
        regx = "[a,b,c]"; //是否匹配我列表中所列举的字符
        regx = "[a-z]"; //列举26个小写字符
        regx = "[A,B,C,D]";
        regx = "[A-Z]";
        regx = "[a-zA-Z]";
        regx = "[^a-zA-Z]"; //不是列表中所列举的
        regx = "[0,1,2]";
        regx = "[0-9]";
        regx = "[a-zA-Z0-9_%]";
        // . 可以匹配单个任意字符
        regx=".";
        regx="..";
        // 我只想要匹配点本身 需要转意
        regx = "\\.";
        // | 表示或者
        regx="|";
        regx = "\\|"; //转意
        regx="a|b";
        regx="\\d"; //跟 [0-9] 意思一样
        regx="\\w"; // \w 单词字符:[a - zA - Z_0 - 9]
        regx = "\\W";  //[^a - zA - Z_0 - 9]
        regx = "\\D"; //跟 [^0-9] 意思一样
        regx="  ";
        regx="\\s"; //匹配空格
        regx="\\S"; //不是空格

        boolean b = "a".matches(regx);

        System.out.println(b);
    }
}

package org.westos.java17;

public class Test5 {
    public static void main(String[] args) {
        //+ 一个或多个
        String regx = "[0-9]+";
        regx = "a+b+";
        // * 零次或多次
        regx = "a*";
        //? 一次或一次也没有 比如""空串 就是没有
        regx = "b?";
        regx="\\w+";
        //正好几次
        regx="a{5}";
        regx="[0-9]{5}";
        //  大于等于5 小于等于15
        regx = "[0-9]{5,15}";
        //至少5位
        regx = "[0-9]{5,}";
        regx="^a.+";
        boolean b = "abcsdfsdfsdfsdf".matches(regx);

        System.out.println(b);
    }
}

package org.westos.java17;
//定义正则表达式

//需求:校验qq号码.
//1:要求必须是5 - 15 位数字
//2:0 不能开头

//校验手机号的规则
//手机号是 11位 每一位是数字 以1开头 第二位是 3 5 6 7 8 其余位是任意数字

//6~18个字符,可使用字母、数字、下划线,需要以字母开头
// westos@163.com   abcdhaha@163.com  abc123@163.com ab_c123@163.com
//网易邮箱的规则
public class Test7 {
    public static void main(String[] args) {
        String regx="[1-9][0-9]{4,14}"; //qq号码
        regx="[1][3,5,6,7,8][0-9]{9}"; //手机号
        regx="[a-zA-Z]\\w{5,17}@163\\.com"; //网易邮箱
    }
}
package org.westos.demo2;

public class MyTest3 {
    public static void main(String[] args) {
        //定义正则表达式
          /* 需求:校验qq号码.
        1:要求必须是5 - 15 位数字
        2:0 不能开头*/
        String regx = "[1-9][0-9]{4,14}";

        //这个数据是否匹配正则表达式
        boolean b = "asfasdf".matches(regx);

        //校验手机号的规则
        //手机号是 11位 每一位是数字 以1开头 第二位是 3 5 6 7 8 其余位是任意数字
        String phoneRegx="[1][3,5,6,7,8][0-9]{9}";
        boolean f = "13992568959".matches(phoneRegx);
        System.out.println(f);

          //6~18个字符,可使用字母、数字、下划线,需要以字母开头
         //  westos@163.com   abcdhaha@163.com  abc123@163.com ab_c123@163.com
        //网易邮箱的规则

        String mailRegx="[a-zA-Z]\\w{5,17}@163\\.com";
        boolean matches = "Westo_163s@163.com".matches(mailRegx);
        System.out.println(matches);

         String mailRegx2="[a-zA-Z]\\w{5,17}@[a-z]{2,16}\\.(com|cn|net|org)";

         //身份证正则
        String regx3="[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]";

        //车牌号正则
        String car="[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}";

        //中文正则
        String chinese="[\\u4e00-\\u9fa5]{2,4}";

        boolean matches1 = "王老虎".matches(chinese);
        System.out.println(matches1);

        //可以是字母也可以中文名

        String username = "([a-zA-Z]{6,16})|([\\u4e00-\\u9fa5]{2,4})";

        boolean matches2 = "欧阳非非".matches(username);
        System.out.println(matches2);

    }
}

三、正则表达式的判断功能

A:正则表达式的判断功能
	String类的功能:public boolean matches(String regex)
B:案例演示:	判断手机号码是否满足规则

四、正则表达式的分割功能

A:正则表达式的分割功能  split()方法
	String类的功能:public String[] split(String regex)
B:案例演示
	年龄范围"18-24"
package org.westos.java18;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        String regex="[\\u4e00-\\u9fa5]{2,4}";//中文正则
        String s="王老虎";
        boolean matches = s.matches(regex); // matches方法判断字符串是否符合该正则表达式
        System.out.println(matches); //true
        String s1="123==sdf123112sdfs=sdfs45556sdfsd=f=s=df1233";
        regex="[=a-z]+";
        String[] split = s1.split(regex); // split方法分割字符串返回一个字符串数组
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }
        System.out.println(Arrays.toString(split)); //[123, 123112, 45556, 1233]
    }
}

五、把给定字符串中的数字排序

A:案例演示:	需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
package org.westos.java18;

import java.util.Arrays;
import java.util.StringJoiner;

//A:案例演示:	需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
public class Test1 {
    public static void main(String[] args) {
        String str = "91      27    46 38 50    102 36 3 260 3 9";
/*        String str="91 27 46 38 50";*/
        String[] split = str.split("\\s+");
        int[] ints=new int[split.length];
        for (int i = 0; i < split.length; i++) {
            ints[i]=Integer.parseInt(split[i]);
        }
        Arrays.sort(ints);
        StringJoiner stringJoiner = new StringJoiner(" ");
        for (int i = 0; i < ints.length; i++) {
            stringJoiner.add(String.valueOf(ints[i]));
        }
        System.out.println(stringJoiner.toString());
    }
}

package org.westos.demo3;

import java.util.Arrays;

public class MyTest2 {
    public static void main(String[] args) {
       /* A:
        案例演示:
        需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”*/
        String str = "91      27    46 38 50    102 36 3 260 3 9";
        //1.截取字符串中的元素
        String[] strings = str.split("\\s+");
        //你 排序字符串数组,是按照字典顺序排序的;
        //Arrays.sort(strings);
        //System.out.println(Arrays.toString(strings));
        int[] arr = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            arr[i] = Integer.parseInt(strings[i]);
        }
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        //遍历数组拼串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]).append(" ");
        }
        String s = sb.toString().trim();
        System.out.println(s);
    }
}

六、正则表达式的替换功能

A:正则表达式的替换功能
	String类的功能:public String replaceAll(String regex,String replacement)
B:案例演示
	论坛发表帖子,帖子中需要将数字替换为"*"
package org.westos.java18;
/*B:案例演示
        论坛发表帖子,帖子中需要将数字替换为"*"*/
public class Test2 {
    public static void main(String[] args) {
        String s = "jsslfaj123sdfasd8f3as1d3fsd123";
/*        String replace = s.replace("ss", "fsdf");
        System.out.println(replace);
        String a = s.replace('a', '=');
        System.out.println(a);*/
        s=s.replaceAll("[0-9]","*");
        System.out.println(s); //jsslfaj***sdfasd*f*as*d*fsd***
    }
}
package org.westos.java18;

import java.util.Arrays;

public class Test3 {
    public static void main(String[] args) {
        //获取下面的字符串中是 三个字母组成的单词
        String str="da jia ting wo shuo, jin tian yao xia yu, bu   , shang, wan zi xi,  , gao xing bu?";
        String[] split = str.split("[^a-zA-Z]+");
        System.out.println(Arrays.toString(split));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            if(split[i].length()==3){
                System.out.println(split[i]);
                sb.append(split[i]).append(" ");
            }
        }
        String[] s = sb.toString().split(" ");
        System.out.println(Arrays.toString(s));
    }
}
package org.westos.java18;

import java.util.Arrays;

public class Test4 {
    public static void main(String[] args) {
        String str="da jia ting wo shuo, jin tian yao xia yu, bu   , shang, wan zi xi,  , gao xing bu?";
        String s1 = str.replaceAll("[^a-z]+", "=");
        System.out.println(s1); //da=jia=ting=wo=shuo=jin=tian=yao=xia=yu=bu=shang=wan=zi=xi=gao=xing=bu=
        String s2 =str.replaceAll("[^a-z]","=");
        System.out.println(s2); //da=jia=ting=wo=shuo==jin=tian=yao=xia=yu==bu=====shang==wan=zi=xi=====gao=xing=bu=
        String[] split = s2.split("=");
        //[da, jia, ting, wo, shuo, , jin, tian, yao, xia, yu, , bu, , , , , shang, , wan, zi, xi, , , , , gao, xing, bu]
        System.out.println(Arrays.toString(split));
        String[] split1 = s2.split("=+");
        //[da, jia, ting, wo, shuo, jin, tian, yao, xia, yu, bu, shang, wan, zi, xi, gao, xing, bu]
        System.out.println(Arrays.toString(split1));
    }
}

七、Pattern和Matcher的概述

正则的获取功能需要使用的类

A:Pattern和Matcher的概述
B:模式和匹配器的典型调用顺序
	通过JDK提供的API,查看Pattern类的说明

	典型的调用顺序是 
	Pattern p = Pattern.compile("a*b");
	Matcher m = p.matcher("aaaaab");
	boolean b = m.matches();
package org.westos.demo4;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyTest {
    public static void main(String[] args) {
        //Pattern  模式器
        //Macher 匹配器
        //模式器:从来封装一个正则表达式
        Pattern p = Pattern.compile("a*b");
        //通过模式器获取匹配器,并且还要传入匹配的数据
        Matcher m = p.matcher("aaaaab");
        //调用匹配器中匹配的方法,进行匹配
        boolean b = m.matches();
        System.out.println(b);


        //如果我们仅仅只是想要知道一个字符串是否符合我们定义的正则,就调用String类中的matches("a*b")方法
        boolean b1 = "aaaaab".matches("a*b");
    }
}

package org.westos.java18;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test5 {
    public static void main(String[] args) {
        Pattern compile = Pattern.compile("[0-9]+");
        Matcher matcher = compile.matcher("12pp");
        boolean matches = matcher.matches();
        System.out.println(matches);
        boolean matches1 = "12pp".matches("[0-9]+"); //等价
        System.out.println(matches1);
    }
}

八、正则表达式的获取功能

A:正则表达式的获取功能
	Pattern和Matcher的结合使用
B:案例演示  使用的是 find()方法  和 group()方法  注意一定要先使用find()方法先找到 才能用group()方法获取出来
	需求:获取下面这个字符串中由三个字符组成的单词 正则表达式 \\b[a-z]{3}\\b
	da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
package org.westos.java18;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*A:正则表达式的获取功能
        Pattern和Matcher的结合使用
        B:案例演示  使用的是 find()方法  和 group()方法  注意一定要先使用find()方法先找到 才能用group()方法获取出来
        需求:获取下面这个字符串中由三个字符组成的单词 正则表达式 \\b[a-z]{3}\\b
        da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?*/
public class Test6 {
    public static void main(String[] args) {
        String str = "da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
        String regx="\\b[a-zA-Z]{3}\\b"; // \\b 单词边界
      /*  boolean find ()
        尝试查找与该模式匹配的输入序列的下一个子序列。

        String group ()
        返回由以前匹配操作所匹配的输入子序列。
*/
        Pattern compile = Pattern.compile(regx);
        Matcher matcher = compile.matcher(str);
        while(matcher.find()){
            String group = matcher.group();
            System.out.println(group);
        }
    }
}

九、Math类概述和方法使用

A:Math类概述
	Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。 
B: 成员变量
	public static final double E : 		自然底数
	public static final double PI:		圆周率
C:成员方法
	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 double random()	获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
	public static int round(float a) 四舍五入
	public static double sqrt(double a)获取正平方根
C:案例演示:	Math类的成员方法使用
package org.westos.java18;
/*
*  成员变量
public static final double E : 		自然底数
public static final double PI:		圆周率
C:成员方法
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 double random()	获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
public static int round(float a) 四舍五入
public static double sqrt(double a)获取正平方根
*
* */
public class Test8 {
    public static void main(String[] args) {
        double e = Math.E;
        double pi = Math.PI;
        int abs = Math.abs(-5);
        System.out.println(abs); //5
        System.out.println(Math.ceil(5.6)); //6.0
        System.out.println(Math.floor(5.6)); //5.0
        int max = Math.max(5, 6);
        double min = Math.min(5.6, 6.9);
        double pow = Math.pow(2, 3);
        System.out.println(pow); //8.0
        double random = Math.random();
        long round = Math.round(5.6);
        System.out.println(round); //6
        double sqrt = Math.sqrt(4);
        System.out.println(sqrt); //2.0
        //求8的立方根
        // public static double pow ( double a, double b)获取a的b次幂

        double pow1 = Math.pow(8, 1 / 3.0);
        System.out.println(pow1); //2.0
    }
}

package org.westos.java18;
//随机生成6位验证码
public class Test9 {
    public static void main(String[] args) {
        //int i=(int)(Math.random()*10);
        for (int i = 0; i < 20; i++) {
            System.out.println(checkcode());
        }
    }
    public static String checkcode(){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            stringBuffer.append((int)(Math.random()*10));
        }
        return stringBuffer.toString();
    }
}

十、Random类的概述和方法使用

A:Random类的概述
	此类用于产生随机数如果用相同的种子创建两个 Random 实例,
	则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
B:构造方法
	public Random()			 没有给定种子,使用的是默认的(当前系统的毫秒值)
	public Random(long seed)		 给定一个long类型的种子,给定以后每一次生成的随机数是相同的
C:成员方法
	public int nextInt()//没有参数 表示的随机数范围 是int类型的范围
	public int nextInt(int n)//可以指定一个随机数范围 
	void nextBytes(byte[] bytes)  生成随机字节并将其置于用户提供的空的 byte 数组中。 
D:案例演示
	Random类的构造方法和成员方法使用
package org.westos.java18;

import java.util.Arrays;
import java.util.Random;

public class Test10 {
    public static void main(String[] args) {
        Random random = new Random();
        for (int i = 0; i < 20; i++) {
            int nextInt = random.nextInt();
            System.out.println(nextInt);
        }
        for (int i = 0; i < 20; i++) {
            int nextInt = random.nextInt(100);//0-99
            System.out.println(nextInt);
        }
        Random random1 = new Random(1); //生成的随机数是固定序列
        for (int i = 0; i < 10; i++) {
            int nextInt = random1.nextInt(10); //0-9
            System.out.println(nextInt);
        }
        byte[] bytes = new byte[10];
        random.nextBytes(bytes);
        System.out.println(Arrays.toString(bytes)); //随机填满字节数组
    }
}

十一、System类的概述和方法使用

A:System类的概述
	System 类包含一些有用的类字段和方法。它不能被实例化。 
B:成员方法
	public static void gc()//调用垃圾回收器
	public static void exit(int status)//退出java虚拟机 0 为正常退出 非0为 异常退出
	public static long currentTimeMillis()//获取当前时间的毫秒值
C:案例演示:	System类的成员方法使用
package org.westos.demo2;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class SystemDemo {
    public static void main(String[] args) {
        //in
        //public static final InputStream in“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入
        InputStream in = System.in;

        Scanner scanner = new Scanner(in);

      /*  out
        public static final PrintStream out“标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器*/
        PrintStream out = System.out;
        out.println("abc");

       // System.out.println();
    }
}
package org.westos.java18;

public class Test11 {
    public static void main(String[] args) {
        //获取系统当前时间的毫秒值
        //获取的是从 1970 1 1 0:0:0 到 现在的一个时间间隔,单位是毫秒
        long l = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            System.out.println(i);
        }
        long l1 = System.currentTimeMillis();
        //计算一段程序运行的时间
        System.out.println(l1-l); //1s=1000ms
    }
}

package org.westos.java18;

public class Test12 {
    public static void main(String[] args) {
        System.out.println(123);
        System.out.println(123);
        //退出虚拟机
        System.exit(0); //0 正常退出,非0强制退出
        //下面的代码不会运行
        System.out.println(1);
        System.out.println(1);
        System.out.println(1);

        Integer integer = new Integer(10);
        int[] ints = new int[20];
        //运行垃圾回收器 并不是说立马会被回收,只是起到提醒回收的作用
        System.gc();
    }
}

十二、BigDecimal类的概述和方法使用

A:BigDecimal的概述
	由于在运算的时候,float类型和double很容易丢失精度,演示案例。
	所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal

	不可变的、任意精度的有符号十进制数。
B:构造方法
	public BigDecimal(String val)
C:成员方法
	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 取舍模式 比如四舍五入
D:案例演示
	BigDecimal类的构造方法和成员方法使用
package org.westos.java18;

import java.math.BigDecimal;

public class Test13 {
    public static void main(String[] args) {
        double a=10;
        double b=3;
        System.out.println(a/b); //3.3333333333333335
        a=10.56566;
        b=3.999999565656;
        System.out.println(a*b); //42.262635410868974
        BigDecimal aa = new BigDecimal("3.999999565656");
/*        System.out.println(aa);*/
        BigDecimal bb = new BigDecimal("10.56566");
/*        System.out.println(bb);*/
        BigDecimal multiply = aa.multiply(bb);
        System.out.println(multiply.toString()); //42.26263541086897296
/*        System.out.println(aa.add(bb).toString());
        System.out.println(bb.subtract(aa).toString());*/
        System.out.println(aa.divide(bb,20,BigDecimal.ROUND_CEILING).toString());
    }
}

十三、补充 BigInteger概述 可以让超过long范围内的数据进行运算

构造方法
public BigInteger(String val)
成员方法
public BigInteger add(BigInteger val)
public BigInteger subtract(BigInteger val)
public BigInteger multiply(BigInteger val)
public BigInteger divide(BigInteger val)
public BigInteger[] divideAndRemainder(BigInteger val)

示例
 BigInteger bi1=new BigInteger("100");
 BigInteger bi2=new BigInteger("2");
        
System.out.println(bi1.add(bi2));   //+
System.out.println(bi1.subtract(bi2));   //-
System.out.println(bi1.multiply(bi2));   //*
System.out.println(bi1.divide(bi2));    //(除)
        
 BigInteger[] arr=bi1.divideAndRemainder(bi2);    //取除数和余数
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
package org.westos.java18;

import java.math.BigInteger;
import java.util.Arrays;

public class Test14 {
    public static void main(String[] args) {
        long maxValue = Long.MAX_VALUE; //9223372036854775807
        System.out.println(maxValue);
/*        maxValue=maxValue+1;
        System.out.println(maxValue);*/
        BigInteger bigInteger = new BigInteger(maxValue + "");
        BigInteger bigInteger1 = new BigInteger("10");
/*        BigInteger multiply = bigInteger.multiply(bigInteger1);
        System.out.println(multiply);*/
        System.out.println(bigInteger.divide(bigInteger1)); //922337203685477580  取的是商

        BigInteger[] arr=bigInteger.divideAndRemainder(bigInteger1);    //取除数和余数
        System.out.println(arr.length); //2
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
        System.out.println("================================================");
        BigInteger[] bigIntegers = bigInteger.divideAndRemainder(bigInteger1);
        System.out.println(Arrays.toString(bigIntegers)); //[922337203685477580, 7]
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值