1. 程式人生 > 實用技巧 >JAVA筆記整理-Math、定時器、大資料(BigDecimal)型別

JAVA筆記整理-Math、定時器、大資料(BigDecimal)型別

一、Math類

java.lang.Math類用於數學計算的工具類 ,它提供都是靜態方法 ,不需要構造Math物件

常用方法:

​ Math.random():獲取隨機數

​ Math.abs() 獲取絕對值

​ Math.ceil(): 向上取整

Math.floor() :向下取整

Math.rint():取接近它的整數 ,如果兩個同樣接近,往偶數靠近

Math.max(int,int):返回兩個數的最大值

Math.min(int,int):返回兩個數的最小值

Math.round():四捨五入整數

Math .sqrt():對一個數開平方

Math.pow(double,double),對一個數的幾次冪

 public static void main(String[] args) {
        System.out.println(Math.PI);
        System.out.println(Math.E);
        System.out.println(" 一個數的絕對值:"+Math.abs(-100));//100
        System.out.println(" 向上取整:"+Math.ceil(13.5)); // 14
        System.out.println(" 向上取整(比它大的最近數):"+Math.ceil(-13.5));// -13
        System.out.println("向下取整:"+ Math.floor(13.5));// 13
        System.out.println("向下取整"+Math.floor(-20.2));// -21

        //四捨五入
        System.out.println(Math.round(23.4));// 23
        System.out.println(Math.round(-23.5));//-23
        System.out.println(Math.round(-24.5));//-24

        System.out.println(Math.rint(23.4));// 23
        System.out.println(Math.rint(23.5));//24 如果兩個一樣接近,取接近偶數
        System.out.println(Math.rint(22.5));//22
        System.out.println(Math.rint(0.6));//0

    }

二、定時器類(Timer類)

​ 1、Timer類

​ java.util.Timer類 是一個任務排程的定時器類,可以安排任務一次執行,或定期重複執行

​ 建立Timer物件

 Timer timer = new Timer();
 Timer timer = new Timer(name);

​ 常用方法

timer.schedule(TimerTask,2000); 2秒後執行一次任務

timer.schedule(TimerTask,2000,1000); 2秒後開始執行任務,每1s執行一次

2、TimerTask類

​ java.util.TimerTask類是由定時器執行的任務類,是一個抽象類。

import java.util.Timer;
import java.util.TimerTask;

public class TestTimer {
    //統計列印次數
    static int count=0;
    // 建立一個定時器類
    static Timer timer = new Timer("我的定時器");

    public static void main(String[] args) {

        // 開始定時一個任務
        TestTimer obj = new TestTimer();
        // 多少毫秒之和 執行一次任務
      //  timer.schedule(obj.new MyTask(),2000);
        // 2秒之後開始執行,每隔1s執行一次
        timer.schedule(obj.new MyTask(),2000,1000);

    }
    // 定義內部類時,可以直接使用外部類的屬性  和它的物件
    class MyTask extends TimerTask{
        @Override
        public void run() {
            System.out.println("你好!!!");
            count++;

            if(count>=10){
                System.out.println("停止");
                //停止
                timer.cancel();
            }
        }
    }
}

三、大資料型別BigDecimal

1、BigDecimal類

兩個double型別的計算可能導致精度不準確,這裡使用

java.math.*裡面提供了BigDecimal類(提供高精度計算的方法)

public static void main(String[] args) {
         double a= 1.200000;
         double b= 1.35433;
         double c = a+b;
        System.out.println(c);
        System.out.println(0.05+0.01);
        System.out.println(1.0-0.42);  // 會出現精度問題  計算不準確

        // 使用BigDecimal ,先將型別轉成字串 (為了避免精度問題)
        BigDecimal num1 = new BigDecimal("0.051");
        BigDecimal num2 = new BigDecimal("0.012");
        // 兩個數相加

        BigDecimal sum = num1.add(num2 ) ;
        // 設定保留2位整數  四捨五入
        sum =sum.setScale(2,BigDecimal.ROUND_HALF_UP);
        System.out.println(sum);

        // 減法
       sum =  num1.subtract(num2);
        System.out.println(sum);

       // 乘法
       sum =  num1.multiply(num2);
        System.out.println(sum);

       // 除法
       sum = num1.divide(num2,2,BigDecimal.ROUND_HALF_UP);
        System.out.println(sum);

    }

2、NumberFormat類

java.text.NumberFormat類 :用於將數值格式轉成指定格式並輸出字串形式的類 。

DecimalFormat類: 屬於NumberFormat的子類。

  // NumberFormat類是對數值型別的格式化類,其中 DecimalFormat是繼承NumberFormat
        // 獲取數值的貨幣表現形式
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        String s1 = nf.format(23424.2);
        System.out.println(s1);

        //獲取數值的百分比
        nf = NumberFormat.getPercentInstance();
        s1= nf.format(0.654);
        System.out.println(s1);

        //根據指定的格式匹配百分比
        nf = new DecimalFormat("##.##%");
        s1=nf.format(0.654);
        System.out.println(s1);

        // 根據指定的模式匹配千分比
        nf = new DecimalFormat("##.##\u2030");
        s1 = nf.format(0.6543);
        System.out.println(s1);

        // 根據指定的模式 轉成科學計數法
        nf = new DecimalFormat("#.###E0");
        s1 = nf.format(198200);
        System.out.println(s1);

        // 根據指定的模式  將字串轉成 指定格式數值型
        String s2 ="25.3%";
        nf = new DecimalFormat("##.##%");
        Number dd = nf.parse(s2);
        double num = (double)dd;
        System.out.println("這個字串轉成double:"+num);