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

来源:互联网 发布:会员数据分析 编辑:程序博客网 时间:2024/06/06 11:02
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

               
原创粉丝点击