数字处理类 知识点

package lianxi;

import java.util.*;
public class shuzichulilei {

    public static void main(String[] args) {
    
        //数字处理类
        //静态方法:不需要类实例化(new)就可以调用的方法
        
        System.out.println(Math.PI);//圆周率的常量
        
        //取整数
        
        double d=123.45;
        
        //1.四舍五入
        System.out.println("四舍五入="+Math.round(d));//只看小数点后面第一位
        
        //保留小数点后几位
        
        System.out.println("四舍五入="+Math.round(d*100)/100.0);
        
        //2.取下限值:小于或等于它的最大整数
        System.out.println("取下限值="+Math.floor(d));
        
        //3.取上限值:大于或等于它的最小整数
        System.out.println("取上限值="+Math.ceil(d));
        
        //随机数(只能出小于1的数)
        Math.random();
        for(int i=0;i<5;i++){
        System.out.println("随机数1="+Math.random());
        }
        System.out.println("随机数2="+Math.random());
        
        //随机数字
        //实例化
        //随机数种子,根据种子值计算出一定的随机数序列
        Random ran=new Random();//括号里面输种子
        
        for(int i=0;i<5;i++){
        int ran1=ran.nextInt(100);
        System.out.println("随机数3="+ran1+"+"+(ran1+1));//括号里面输范围
        }
        //数据类型转换
        Double dd=new Double ("123.45");
        System.out.println(dd);
        //从包装类转换成基本类型
        double dv =dd.doubleValue();
        System.out.println(dv);
        
        //转成字符串
        Double df =new Double(1234.56);
        String ds=df.toString();
        System.out.println("ds="+ds);
        Float f=new Float("123.4");
        String ff=f.toString();
        Integer i=new Integer("123");//int的包装类
        int ii=Integer.valueOf("1234").intValue();
        //布尔型
        Boolean b=new Boolean("true");//只要是不区分大小写的true输出true,其他的都是false
        System.out.println("boolean="+b.booleanValue());
        
        
    }
}
原文地址:https://www.cnblogs.com/zhanghaozhe8462/p/5132231.html