BigDecimal类;数字格式;Random类;Math类

BigDecimal类

属于大数据类,精度极高,不属于基本数据类型,属于java对象(引用数据类型),专门用在财务软件中,财务软件中double是不够的。

public static void f1(){
    
    
    //这个100不是普通的100,是精度极高的100
    BigDecimal v1 = new BigDecimal(100);
    //精度极高的200
    BigDecimal v1 = new BigDecimal(100);
    //求和
    //v1+v2不行,都是引用,不能直接使用+求和
    BigDecimal v3 = v1.add(v2);//调用方法求和
    System.out.println(v3);//300
    BigDecimal v4 = v2.divide(v1);
    System.out.println(v4);//2
 }

数字格式

#代表任意数字

,代表千分位

.代表小数点

0代表不够时补0

DecimalFormat df = new DecimalFormat("###,###.##");
String s = df.format(1234.561232);
System.out.println(s);//"1,234.56"
DecimalFormat df2 = new DecimalFormat("###,###.0000");
String s2 = df2.format(1234.56);
System.out.println(s);//"1,234.5600"

Random类

public static void main(String[] args) {
    
    
		// 10-99
		//方法1
		Random r=new Random();
		for(int i=1;i<=10;i++)
			System.out.print(	(r.nextInt(90)+10)+"  " );//12  48  15  72  41  32  22  92  33  95  
		System.out.println();
		//方法2
		for(int i=1;i<=10;i++)
			System.out.print(  ((int)( Math.random()*90)+10)+"  ");//20  79  17  13  73  86  35  60  75  73
	}

Math类

public static void main(String[] args) {
    
    
	 //Math 类的构造方法是私有的不能生成对象,里面的方法都是静态的,通过类名访问,不需要生成对象
		//Math.random() 随机(0,1)小数
		System.out.println(Math.random()*10);
		
		System.out.println(Math.floor(3.5));//3.0 地板 向下
		System.out.println(Math.ceil(3.5));;//4.0 天花板 向上
		System.out.println(Math.round(3.5));//4四舍五入
		
		System.out.println(Math.floor(3.3));//3.0 地板 向下
		System.out.println(Math.ceil(3.3));;//4.0 天花板 向上
		System.out.println(Math.round(3.3));//3四舍五入
		
		System.out.println(Math.pow(3, 4));//81
	}

猜你喜欢

转载自blog.csdn.net/Werdio/article/details/113094437