import java.text.NumberFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class MathMethodService { public static void main(String[] args) { //取整和極值 System.out.println("-1.8向下取整:"+Math.floor(-1.8)); System.out.println("2.3向上取整:"+Math.ceil(2.3)); System.out.println("2.45四捨五入取整:"+Math.round(2.45)); System.out.println("-2.56的絕對值:"+Math.abs(-2.56)); System.out.println("最接近2.55的整數(若是恰好居中則取偶數):"+Math.rint(2.55)); System.out.println("-2.3的符號值:"+Math.signum(-2.3)); //符號函數 System.out.println("10除以3的餘數:"+Math.IEEEremainder(10, 3)); System.out.println("2和4的最大值:"+Math.max(2, 4)); System.out.println("4和7的最小值:"+Math.min(4, 7)); //指數和對數 System.out.println("4的平方根:"+Math.sqrt(4)); System.out.println("27的立方根:"+Math.cbrt(27)); System.out.println("3的2次冪:"+Math.pow(3, 2)); System.out.println("2.3乘以2的3次方:"+Math.scalb(2.3, 3)); System.out.println("sqrt(3的平方加上4的平方):"+Math.hypot(3, 4)); System.out.println("e的3次冪:"+Math.exp(3)); System.out.println("e的3次冪減1:"+Math.expm1(3)); System.out.println("天然底數:"+Math.E); System.out.println("天然底數:"+Math.PI); System.out.println("10的天然對數:"+Math.log(10)); System.out.println("底數爲10的對數:"+Math.log10(10)); System.out.println("log(1+10):"+Math.log1p(10)); //產生隨機數 System.out.println("大於等於0,且小於1的僞隨機數:"+Math.random()); Random random = new Random(); System.out.println("隨機雙精度值:"+random.nextDouble()); System.out.println("標準正態分佈隨機數:"+random.nextGaussian()); System.out.println("隨機布爾值:"+random.nextBoolean()); byte[] randomByte = new byte[16]; random.nextBytes(randomByte); System.out.println("隨機數組:"+Arrays.toString(randomByte)); Random random2 = new Random(System.currentTimeMillis()); //爲避免兩個Random對象產生相同的數字序列,使用當前時間做爲Random對象的種子 System.out.println("隨機布爾值(不重複):"+random2.nextBoolean()); ThreadLocalRandom trand= ThreadLocalRandom.current(); //用多線程產生隨機序列 System.out.println("4到64的僞隨機數"+trand.nextInt(4,64)); //三角函數 System.out.println("弧度值爲10的sine值:"+Math.sin(3.1415926/3)); System.out.println("弧度值爲10的cosine值:"+Math.cos(3.1415926/2)); System.out.println("弧度值爲10的tangent值:"+Math.tan(3.1415926/4)); System.out.println("反正弦:"+Math.asin(0)); System.out.println("反餘弦:"+Math.acos(0)); System.out.println("反正切:"+Math.atan(0)); System.out.println("通過點(10,20)與原點的的直線和通過點(0,0)與原點的直線之間所成的夾角:"+Math.atan2(10-0, 20-0)); System.out.println("雙曲正弦函數(exp(10)-exp(-10))/2.0:"+Math.sinh(10)); System.out.println("轉爲角度:"+Math.toDegrees(3.1415926/3)); System.out.println("轉爲弧度:"+Math.toRadians(60)); //調整數值格式 NumberFormat nf = NumberFormat.getInstance(); //實例化一個NumberFormat對象 nf.setMaximumFractionDigits(5); //設置小數部分容許的最大位數 nf.setMaximumIntegerDigits(4); //設置整數部分容許的最大位數 nf.setMinimumFractionDigits(2); //設置小數部分容許的最小位數 nf.setMinimumIntegerDigits(2); //設置整數部分容許的最小位數 System.out.println(nf.format(123.1415926)); System.out.println(nf.format(123.1)); System.out.println(nf.format(12345.1)); System.out.println(nf.format(1.1));