目录
一、数字格式化
DecimalDormat类
构造方法:DecimalFormat df=newDecimalFormat(“###”); //###:格式化模板
DecimalFormat df=new DecimalDormat();
df.applyPattern(“####”);
package 数字结构;
import java.text.DecimalFormat;
import java.util.Scanner;
public class DECIMALFORMAT {
static void a(String a,Double b) {
DecimalFormat df=new DecimalFormat(a);
System.out.println(a);
System.out.println(b);
System.out.println(df.format(b));
System.out.println();
}
public static void main(String[] args) {
a("000",123.0);
a("##,###,###.##$",12345678.21);
a("0.00\u2030",0.1234);
// TODO 自动生成的方法存根
}
}
二、Math类
1.常用的Math下的方法
java.lang.Math
- java.lang包不需要引入,系统会自动调用;
- Math类提供的方法都是静态方法;
2、Math.random方法
Math.random() 返回double值 // 0<=double值<=1
Math.random()*m 返回double值 // 0<=double值<=m
3、Random类
1、语法:Random r=new Random()
2、nextInt/nextLong/nextDoouble/nextFloat/nexrBoolean / 返回随机生成int/long/Double/Float/Boolean值
三、BigInteger类
- 大数字BigInteger类——任意精度;
- 构造方法:BigInteger i=new BigInteger(“999999”);
BigInteger i=new BigInteger.valueOf(9999L);
package 数字结构;
import java.math.BigInteger;
public class BIGINTEGER {
public static void main(String[] args) {
BigInteger i1=new BigInteger("99");
BigInteger i2=BigInteger.valueOf(99999L);
System.out.println("加法"+i1.add(new BigInteger("2")));
System.out.println("减法"+i1.subtract(new BigInteger("2")));
System.out.println("乘法"+i1.multiply(new BigInteger("2")));
System.out.println("除法"+i1.divide(new BigInteger("2")));
System.out.println("求余"+i1.remainder(new BigInteger("2")));
System.out.println("求幂"+i1.pow(10));
System.out.println("比较大小:"+i1.compareTo(i2));
//如果i1>12输出正数,i1=i2输出0,i1<i2输出负数
System.out.println("比较相等"+i1.equals(new BigInteger("99")));//输出Boolean
System.out.println("取最大值"+i1.max(i2));
System.out.println("取最小值"+i1.min(i2));
BigInteger a=new BigInteger("80");
BigInteger b=new BigInteger("140");
System.out.println(a+"与"+b+"最大公约数是:"+a.gcd(b));
// TODO 自动生成的方法存根
}
}
四、BigDecimal类
(与BigInteger的区别:可以使用任意精度的小数)
- 构造方法:BigDecimal d=new BigDecimal(“123.45”);
BigDecimal d=new BigDecimal(123.45);
BigDecimal d=new BigDecimal.valueOf(123.45);
- 常用方法:与BigInteger基本相同
divide(BigDecimal divisor,int scale,int roundingMode)
//除数,小数位数,近似值处理模式)
- 近似处理模式: