Java基础(三十五)Math、Random类和数字格式化(String.format方法)
一、Math類
Math類常用的方法:
public static long abs (double a) 返回a的絕對值 public static double max (double a,double b) 返回a、b的最大值 public static double min (double a,double b) 返回a、b的最小值 public static double pow (double a,double b) 返回a的b次冪 public static double sqrt (double a) 返回a的平方根 public static double log (double a) 返回a的對數(shù) public static double sin (double a) 返回a的正弦值 public static double asin (double a) 返回a的反正弦值 public static double random() 產(chǎn)生一個0到1之間的隨機數(shù),這個隨機數(shù)不包括0和1二、Random類
1.兩種構(gòu)造方法
public Random() // 不含參數(shù)的構(gòu)造方法以當(dāng)前時間作為種子,不同時間運行的結(jié)果不同 pbulic Random(long seed) // 而含參的構(gòu)造方法以種子為基礎(chǔ)計算隨機數(shù),不同時間以相同順序執(zhí)行結(jié)果一樣,兩個具有相同種子的Random對象按相同順序執(zhí)行結(jié)果也一樣。2.使用方法
package integer.jun.iplab;import java.util.Random;public class RandomTest {public static void main(String[] args) {System.out.println(Math.random()); // 返回一個0(不包括)到1(不包括)之間的隨機數(shù) Random rd = new Random();System.out.println(rd.nextInt()); // 返回32位表示的數(shù)的補碼形式System.out.println(rd.nextInt(32)); // 返回0(包括)到32(不包括)的隨機數(shù) Random rd_1 = new Random(100);System.out.print(rd_1.nextInt() + " ");System.out.print(rd_1.nextDouble() + " ");System.out.print(rd_1.nextFloat() + " ");System.out.print(rd_1.nextBoolean() + " ");System.out.println();Random rd_2 = new Random(100);System.out.print(rd_2.nextInt() + " ");System.out.print(rd_2.nextDouble() + " ");System.out.print(rd_2.nextFloat() + " ");System.out.print(rd_2.nextBoolean() + " ");} }輸出: 0.557101449009472 1622116987 18 -1193959466 0.7346627443280227 0.7158033 true -1193959466 0.7346627443280227 0.7158033 true?
二、數(shù)字格式化
數(shù)字格式化是指按照指定格式得到一個字符串。
1.Formatter類
(1)格式化模式是format方法中的一個使用雙括號括起來的字符序列,該字符序列由格式和普通字符構(gòu)成。
(2)值列表是使用逗號分隔的變量、常量或表達式。但是,要保證format方法“格式化模式”中格式符的個數(shù)與“值列表”中列出的值的個數(shù)相同。
String m = String.format("%d元%.1f箱%d斤", 78, 8.0, 125); System.out.println(m); // 打印: 78元8.0箱125斤
2.格式化整數(shù)
(1)格式符
%d,%o,%x和X格式符可格式化byte、Byte、short、Short、int、Integer、long和Long型數(shù)據(jù)。 %d將值格式化為十進制整數(shù)。 %o將值格式化為八進制整數(shù)。 %x將值格式化為小寫的十六進制整數(shù)。 %X將值格式化為大寫的十六進制整數(shù)。(2)修飾符
String m1 = String.format("按千分組: %,d; 按千分組帶正號: %+,d", 123456, 654321);System.out.println(m1); // 按千分組: 123,456; 按千分組帶正號: +654,321(3)數(shù)據(jù)的寬度
數(shù)據(jù)的寬度就是返回字符串的長度,“%md”表示在數(shù)字的左面加空格;“%-md”表示在數(shù)字的右面加空格
String m2 = String.format("%6d", 777);System.out.println(m2); // 打印: 空格空格空格777System.out.println(m2.length()); // 打印: 6?
3.格式化浮點數(shù)
(1)格式符
%f,%e(%E),%g(%G)和%a(%A)格式符可格式化float、Float、double和Double。(2)修飾符
String m4 = String.format("整數(shù)部分按千分組: %,f; 整數(shù)部分按千分組帶正號: %+,f", 123456789.123456, 123456789.123456); System.out.println(m4); // 整數(shù)部分按千分組: 123,456,789.123456; 整數(shù)部分按千分組帶正號: +123,456,789.123456(3)限制小數(shù)位數(shù)的“寬度”
"%.nf"表示保留n位小數(shù),“%mf”表示在數(shù)字的左邊加上空格湊成長度為m的字符串,“%-mf”表示在數(shù)字的右邊加上空格湊成長度為m的字符串。
轉(zhuǎn)載于:https://www.cnblogs.com/BigJunOba/p/9439672.html
總結(jié)
以上是生活随笔為你收集整理的Java基础(三十五)Math、Random类和数字格式化(String.format方法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 登录注册的基本加密方法(可逆)
- 下一篇: Java类之File记录