Byte b =100;Short s =100;Integer i =100;Long l =100L;Float f =100.0f;Double d =100.0;Character c ='a';Boolean bool =true;
三、Math工具类
//取绝对值int abs =Math.abs(-10);//求最大值int max =Math.max(1,6);//求最小值int min =Math.min(1,6);//随机数(大于等于0.0小于1.0之间)int random =(int)(Math.random()*10);//四舍五入long round =Math.round(5.6);
//取绝对值int abs =Math.abs(-10);System.out.println(abs);//求最大值int max =Math.max(1,6);System.out.println(max);//求最小值int min =Math.min(1,6);System.out.println(min);//随机数int random =(int)(Math.random()*10);System.out.println(random);//四舍五入long round =Math.round(5.6);System.out.println(round);
double a =1.0;double b =0.9;//先转换为包装类,然后转换为字符串String string =Double.valueOf(a).toString();String string1 =Double.valueOf(b).toString().toString();//BigDecimal类 用于精确计算BigDecimal bd =newBigDecimal(string);BigDecimal bd1 =newBigDecimal(string1);//加减System.out.println(bd.add(bd1));System.out.println(bd.subtract(bd1));//乘System.out.println(bd.multiply(bd1));//除(除数取整,保留小数位数,四舍五入方式)System.out.println(bd.divide(bd1,2,BigDecimal.ROUND_HALF_UP));
六、DecimalFormat
//保留两位小数DecimalFormat decimalFormat =newDecimalFormat("###,###.00");//四舍五入
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);//导入对象String format = decimalFormat.format(a);
七、日期类
Date date =newDate();//设置日期格式SimpleDateFormat simpleDateFormat =newSimpleDateFormat("yyyy年MM月dd日--HH:mm:ss");//将接收到的日期格式化String format = simpleDateFormat.format(date);System.out.println(format);//定义个字符串日期String a ="2024年02月19日--10:24:51";//日期格式SimpleDateFormat b =newSimpleDateFormat("yyyy年MM月dd日--HH:mm:ss");//如果字符串日期相同转换为初始状态,不同则报出异常Date date1 = b.parse(a);System.out.println(date1);