java学习之路----java类库----Math类

Math 是数学操作类,提供了一系列的数学操作方法

               看源码会发现Math的所有方法都是静态的,都可以直接调用。

例子:
public   class  MathDemo {
     
            public   static  void  main(String[] args) {
               System.  out  .println(java.lang.Math.sqrt(9.0));  //求平方根
              System.  out  .println(Math.max(10, 5));  //求两个数之间的最大值
              System.  out  .println(Math.max(30, Math.max(10,20)));  //求三个数之间的最大值
              
              System.  out  .println(Math.pow(2, 3));  //2的3次方
              
              System.  out  .println(Math.round(40.6));  //四舍五入
          }

}

结果:
3.0
10
30
8.0
41


Random类


     

public   class  RandomDemo {
     
            public   static  void  main(String[] args) {
              Random r=  new  Random();
                for  ( int  i=0;i<10;i++){
                   System.  out  .print(r.nextInt(100)+ "\t"  );//不大于100的整数
              }
          }

}

结果:
34   23   58   87   79   4    13   78   26   0    

NumberFormat类

                这个类的作用是用来进行数字的格式化的
             public  abstract   class  NumberFormat  extends  Format   
看下源码,发现这个类是抽象类

          
例子
public   class  NumberFormatDemo {
       public   static   void  main(String[] args) {
          NumberFormat format=NumberFormat. getInstance();
          
          System.  out  .println(format.format(10000000000000L));;
          System.  out  .println(format.format(10000000f));
          System.  out  .println(format.format(2000000.009898));
     }

}
结果:
10,000,000,000,000
10,000,000
2,000,000.01

DecimalFormat类

      decimalFormat类是Format的一个子类,在数字格式化方面更加方便



例子:
public   class  DecimalFormatDemo {
            public   void  format1(String partten,  double  values){
              DecimalFormat decimalFormat=  null  ;
              decimalFormat=  new  DecimalFormat(partten); //实例化对象
              String str=decimalFormat.format(values); //格式化数字
              System.  out  .println( "使用"  +partten+  "格式化数字" +values+  ":"  +str);
              
          }
          
              
            public   static  void  main(String[] args) {
               DecimalFormatDemo decimalFormatDemo=  new  DecimalFormatDemo();
              decimalFormatDemo.format1(  "###,###,###"  , 123456789.0000);
              decimalFormatDemo.format1(  "000,000,000"  , 122312313);
              decimalFormatDemo.format1(  "000,000,000¥"  , 123123121);
              decimalFormatDemo.format1(  "00.000%"  , 0.12345);
              decimalFormatDemo.format1(  "###.###\u2030"  , 0.123123);
          }
}

结果:
使用###,###,###格式化数字1.23456789E8:123,456,789
使用000,000,000格式化数字1.22312313E8:122,312,313
使用000,000,000¥格式化数字1.23123121E8:123,123,121¥
使用00.000%格式化数字0.12345:12.345%
使用###.###‰格式化数字0.123123:123.123‰

BigInteger类

           当数字特别大时,可以用这个类来运算

          


例子:
public   class  BigIntegerDemo {
       public   static   void  main(String[] args) {
           BigInteger bigInteger=  new  BigInteger( "123456789999"  ); //定义对象
          
          
          BigInteger bigInteger1=  new  BigInteger( "123456789999"  ); //定义对象
          
          System.  out  .println(bigInteger.add(bigInteger1));  //加法
          
          System.  out  .println(bigInteger.multiply(bigInteger1));  //乘法
          
          System.  out  .println(bigInteger.subtract(bigInteger1));  //减法
          
          System.  out  .println(bigInteger.divide(bigInteger1));  //除法

          System.  out  .println(bigInteger.max(bigInteger1));  //最大数
          
          System.  out  .println(bigInteger.min(bigInteger1));  //最小数
          
          
     }

}

结果:
246913579998
15241578996857186420001
0
1
123456789999
123456789999

BigDecimal类

                如果需要精确的计算则要用这个类



public   class  BigDecimalDemo {
       public   static   void  main(String[] args) {
          BigDecimal b1=  new    BigDecimal(20.99999);
          BigDecimal b2=  new    BigDecimal(19.999999999);
          System.  out  .println(b1.subtract(b2).doubleValue());
     }

}

结果:

0.9999900010000005

               
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值