java成神之——數值操作BigDecimal,BigInteger,Random,SecureRandom


數值操作

數值新特性

123_456 等價於 123456,增加可讀性

包裝類

每個基本數據類型都會有一個包裝類與之對應,用來提供更為強大的功能

包裝類: Byte Short Integer Long Float Double Character Boolean
        
將字符串轉化成對應的基本數據類型
    boolean bl = Boolean.parseBoolean("false");
    int i = Integer.parseInt("11");
            
將基本數據類型轉化成字符串
    String str = Integer.toString(3);
            
使用包裝類構造函數
    Integer it = new Integer("5555");
    int i = it.intValue();
            
基本數據類型自動裝箱和拆箱
    Integer i = 11; // 裝箱
    i++; // 拆箱

浮點

String.format("%.2f", 1.2399);                  // "1.24"
new DecimalFormat("0.##").format(1.2323000);    // 1.23

BigDecimal

高精度計算

BigDecimal方法

    BigDecimal a = new BigDecimal(10000000);
    BigDecimal b = new BigDecimal(10000001);
    a.compareTo(b)                                          // -1      a>b -1, a=b 0, a<b 1
    a.add(b)                                                // 20000001
    a.subtract(b)                                           // -1
    a.multiply(b)                                           // 100000010000000
    a.multiply(b, new MathContext(4,RoundingMode.FLOOR))    // 1.000E+14
    a.divide(b, new MathContext(4,RoundingMode.HALF_DOWN))  // 1.000
    a.divide(b, 4, RoundingMode.HALF_DOWN)                  // 1.000
    a.remainder(b)                                          // 10000000 取模
    a.pow(10)                                               // 次方
    a.max(b)                                                // 比較a,b誰大
    a.min(b)                                                // 比較a,b誰小
    a.movePointLeft(2)                                      // 100000.00 移動小數點
    不要使用equals判斷是否相等,除非數字小數點個數也相同

BigDecimal靜態實例

    BigDecimal a = BigDecimal.ZERO;
    BigDecimal b = BigDecimal.ONE;
    BigDecimal c = BigDecimal.TEN;
    String.format("%s, %s, %s", a, b, c)                    // 0, 1, 10

BigInteger

高精度計算

BigDecimal方法

    BigInteger a = BigInteger.valueOf(Long.MAX_VALUE);
    BigInteger b = new BigInteger("-11");
    BigInteger c = BigInteger.ZERO;
    BigInteger d = BigInteger.ONE;
    BigInteger e = BigInteger.TEN;
    String.format("%s, %s, %s, %s, %s", a, b, c, d, e)     // 9223372036854775807, -11, 0, 1, 10

    a.add(b);
    a.subtract(b);
    a.divide(b);
    a.multiply(b);
    a.pow(3);
    a.remainder(b);
    a.max(b);
    a.min(b);
    a.equals(b);
    不要使用 == 比較
    a.compareTo(b);

返回一個64位數
    BigInteger a = new BigInteger(32, new Random());       // 隨機數,32表示位數,因為返回的是正數所以一共64位
    BigInteger b = new BigInteger(32, new SecureRandom()); // 性能低,但是隨機數質量更高

數值本地化

ISO 639 alpha-2 語言簡寫 https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
ISO 3166 alpha-2 國家簡寫 https://www.iso.org/obp/ui/#search

格式化數值

    Locale locale = new Locale("zh", "CN");
    NumberFormat numberFormat = NumberFormat.getInstance(locale);
    numberFormat.format(1.111)

格式化貨幣

    Locale locale = new Locale("zh", "CN");
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
    numberFormat.format(1.111) // ¥1.11

格式化百分數

    Locale locale = new Locale("zh", "CN");
    NumberFormat numberFormat = NumberFormat.getPercentInstance(locale);
    numberFormat.format(1.111) // 111%

格式化位數

    Locale locale = new Locale("zh", "CN");
    NumberFormat numberFormat = NumberFormat.getInstance(locale);
    numberFormat.setMinimumIntegerDigits(2);
    numberFormat.format(1.111); // 01.111

    numberFormat.setMaximumIntegerDigits(1);
    numberFormat.format(12.111); // 2.111

    numberFormat.setMinimumFractionDigits(1);
    numberFormat.format(12); // 12.0

    numberFormat.setMaximumFractionDigits(1);
    numberFormat.format(12.11) // 12.1

隨機數

假隨機數

Random random = new Random(); 
int randInt = random.nextInt();               // 0-1
int randInt = random.nextInt(1000);           // 0-1000
ThreadLocalRandom.current().nextInt(10, 100); // 10-100

long randLong = random.nextLong();
double randDouble = random.nextDouble();
float randFloat = random.nextFloat();

byte[] bytes = new byte[16];
random.nextBytes(bytes);

真隨機數

SecureRandom rng = new SecureRandom();
byte[] randomBytes = new byte[64];
rng.nextBytes(randomBytes);
Arrays.toString(randomBytes);

播種

相同的種子只能產生相同的隨機數

Random random = new Random(12345L);
Random random = new Random(System.currentTimeMillis());

ThreadLocalRandom tlr = ThreadLocalRandom.current().setSeed(12345L);
ThreadLocalRandom.current().setSeed(System.currentTimeMillis());

結語

本文章是java成神的系列文章之一

如果你想知道,但是本文沒有的,請下方留言

我會第一時間總結出來並發布填充到本文


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM