数学相关常用类

数学相关常用类

  1. 和数学相关的就去Math类
  2. Random专门做随机数
  3. UUID是唯一随机数
  4. BigInteger大整数:比long大,-2的63次方 ~ 2的63次方-1
  5. DecimalFormat格式化小数位数的类

package com.li.changGe.commonlyusedClass;

import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.UUID;

public class MathRelatedDemo01 {
  public static void main(String[] args) {
    //mathTest();
    //randomTest();

    //UUIDTest();

    //10333147966386144929666651337523200000000
    //bigIntegerTest(35);

    decimalFormat();
  }

  public static void mathTest(){
    /**
     * 圆周率和自然对数(复利权限数)
     *
     * 绝对值(正数还是自己,负数去掉-号),
     * 向上取整,向下取整
     * 临近整数,优先偶数
     *
     * 四舍五入
     * 最大,次方和(开方)平方根
     */
    int num = Math.abs(-1);//1

    double result = Math.PI;//3.14159265358979323846;
    result = Math.E;//2.7182818284590452354;

    result = Math.ceil(10.5);//11.0
    result = Math.floor(10.4);//10.0

    result = Math.rint(1.5);//2.0

    result = Math.round(10.5);//11.0

    num = Math.max(10,20);//20

    result = Math.pow(10,3);//1000.0

    result = Math.sqrt(9);//3.0

    /**
     * Math.random()计算小数的时候精确程度可能有些损失
     *
     * 0~0.99.. * 6 + 5 = 0 ~ 10.5
     */
    result = Math.random()*6+5;

  }
    
//----------------------------------------------------------
    
  public static void randomTest(){
    /**
     * nextInt范围必须是正数,不然报错:
     * IllegalArgumentException
     */
    Random random = new Random(-1);
    int num = random.nextInt(10);

    double v = random.nextDouble();

    boolean b = random.nextBoolean();
  }
    
//----------------------------------------------------------
    
  /**
   * UUID通常用做数据库表格主键
   *
   * 产生一个32位随机元素,都是16进制的
   */
  public static void UUIDTest(){
    //UUID uuid1 = new UUID();
    //d9edd214-a033-462a-a55a-7296ea99ffb7
    UUID uuid = UUID.randomUUID();
  }
    
//----------------------------------------------------------
    
  /**
   * 方法和BigDecimal一样
   *
   * 计算阶乘
   */
  public static void bigIntegerTest(int num){
    BigInteger bigInteger = new BigInteger("1");

    for (int i = 1; i <= num; i++) {
      bigInteger = bigInteger.multiply(new BigInteger(i+""));
    }
    System.out.println(bigInteger);
  }
    
//----------------------------------------------------------
    
  public static void decimalFormat(){
    DecimalFormat decimalFormat = new DecimalFormat("000.##");

    /**
     * 不足补位
     * #尽量四舍五入取整,多少个都只保留小数点后一位
     * 013.9
     */
    String format = decimalFormat.format(13.8999);
    System.out.println(format);
  }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helloses

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值