日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 3number_java 数据Number、Math

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 3number_java 数据Number、Math 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個初出茅廬的小子與大家共享一些關于Number和Math的使用,因水平有限,難免有寫的不完善的地方,嘻嘻。看完之后,希望可以留下你珍貴的指導意見。

The Numbers Classes

在寫代碼的時候,也許會使用到java各種的基本數據類型,如下:

int i = 500;

float gpa = 3.65f;

byte mask = 0xff;

然而,在面向對象開發的過程,我們更倡導你使用對象來代替各種的基本數據類型,而java也為各種基本數據類型提供了wrapper class(包裝類)——這些wrapper class封裝了對應的基本數據。通常,編譯器會為我們完成這種封裝——換句話說就是,如果一個方法需要Object類型的參數,而實際提供的值確實2、3等數值,這時編譯器會把2、3等數值封裝成對應的wrapper class;類似,如果一個方法需要基本數據類型的參數,而實際提供的卻似Object類型,這時編譯器會Object類型的參數拆成幾本數據類型——而這就所為的“自動拆裝箱原理”。

接著給出一個自動拆裝箱的demo:Integer x, y;

x = 12;

y = 15;

System.out.println(x+y);以上為x、y變量賦int 值,編譯器會自動將x、y封裝成Integer對象類型;在println()里,為了進行基本的算術處理,編譯器會自動將x、y變量拆成int的基本數據類型。所有基本數據的wrapper class都是抽象類Number的子類

附:在這里,有四個Number的子類并沒有討論。BigDecimal 和 BigInteger 是用于高精度的運算;AtomicInteger 和 AtomicLong 用于多線程程序里。下面給出使用wrapper class 而不使用基本數據類型的理由:1、可用在需要Object類型參數的方法里(常用于操作集合的數據)2、在類里可使用各種常量(如MIN_VALUE和MAX_VALUE),可以提供各種數據的上下界限(范圍)。3、可以使用wrapper class的對象方法,進行各種值與基本數據類型的相互轉換,如String類型的相互轉換,還可以與各種進制的數據進行相互裝換(二進制binary、八進制octal、十進制decimal、十六進制hexadecimal)下表列出了各個wrapper class都具有的方法(Number 類的方法)Methods ?Implemented by all Subclasses of Number

方法描述

byte byteValue()

short shortValue()

int intValue()

long longValue()

float floatValue()

double doubleValue()將Number對象轉換成對應的基本數據類型

int compareTo(Byte anotherByte)

int compareTo(Double anotherDouble)

int compareTo(Float anotherFloat)

int compareTo(Integer anotherInteger)

int compareTo(Long anotherLong)

int compareTo(Short anotherShort)將Number對象的值與參數anotherXxx的值進行比較

boolean equals(Object obj)判斷Number對象和參數Object是否相同。

當兩個對象不為空且數據類型和值都一致的情況下,才返回true。

如果判斷的是Double和Float類型,則還需要一些額外的參數,詳情可參考java API的官方文檔。

對于與String類型的數據與及各種進制數據之間的裝換,每個Number類(在這指其子類)都有著相對應的方法。下表列出了Integer對象與String數據和進制之間相互轉換的方法。其他的Number類都與之類似。Integer類的轉換方法

方法描述

static Integer decode(String s)將String解碼為Integer。String數據可以是十進制、八進制或者十六進制。

static int parseInt(String s)返回一個int值,String只能是十進制的數據。

static int parseInt(String s, int radix)將指定進制的String數據轉換成int值。參數radix指多少進制(可以是2、8、10或者16)

String toString()將Integer數據轉成String類型

static String toString(int i)返回一個指定整數的String對象

static Integer valueOf(int i)返回一個表示指定的int值的Integer實例。

static Integer valueOf(String s)返回String數據的Integer對象

static Integer valueOf(String s, int radix)將指定進制的String數據轉換為Integer類型。如當s=”333”,radix=8時,會返回數據為219的Integer實例。

格式化輸出各種數據

一直以來,也許你都是在使用標準輸出(System.out)輸出各種數據,之所以可以這樣做,是因為所有Number數據都可以轉換成String類型。然而,有時候你可能需要更好地控制數據的輸出,則需要使用java提供的其他方法。

printf()和format()方法在java.io包里,有一個PrintStream類,此類提供了兩個方法(printf()和format())來取代print()和println()。printf和format方法除了名字不一樣之外,其余用法一致。同時因為System.out本事是一個PrintStream對象,所以你可以在原來使用print()和println()的位置用printf()或者format()方法來代替——System.out.format(.....);

其方法原型如下:public PrintStream format(String format, Object... args);public PrintStream printf(String format, Object... args);參數format指定輸出的格式(字符竄的格式),而args則是輸出格式里的數據(參數列表)。為了說明它的用法,可以看一下這個簡單的例子:

System.out.format("The value of "+ "the float variable is " + "%f, while the value of the "

+ "integer variable is %d, " + "and the string is %s", floatVar, intVar, stringVar);

格式說明由“%+轉換符”組成,轉換符是一個指定輸出數據類型的字符。同時,你可以在“%”和轉換符之間有選擇地添加其他標記。想了解更多關于轉換符標記的信息,可以參閱java.util.Formatter。下面給出一個基本用法的例子:int i = 461012;

System.out.format("The value of i is: %d%n", i);

“%d”代表了十進制數據(在這里值i),“%n”代表了輸出換行,所以其輸出如下:The value of i is: 461012為了適應各地語言的使用習慣,java重載了printf和format方法,如下:public PrintStream format(Locale l, String format, Object... args)如在法國,輸出的帶有小數的數據時,是用“,”劃分整數和小數的,如下面的代碼:float floatVar = 123.37f;

int intVar = 123;

StringstringVar = "123";

System.out.format(Locale.FRANCE, "The value of the float " + "variable is %f, while the "

+ "value of the integer variable " + "is %d, and the string is %s%n", floatVar, intVar, stringVar);

其輸出如下:

The value of the float variable is 123,370003, while the value of the integer variable is 123, and the string is123

Demo:格式化輸出

在這個Demo里,使用到各種常用的轉化符和flags,具體解釋如下表:(更多格式可參閱java.util.Formatter)Converters and Flags Used indemo.java

轉換符Flag說明

d十進制數據

f浮點數據

n換行符。注意是用“%n”,而不是“\n”.

tB,tb‘t’指日期/時間的轉換(下同),’B’指月份全稱(如January),’b’指月份的簡稱(如Jan)

ty, tY格式年份,‘y’指格式化時帶前導零的兩位數,即00 – 99;’Y’指格式化時帶前導零的四位數(至少),如0096或者2013。

tm格式月份,’m’指格式時帶前導零的兩位數,即01 ~ 13.

td, te‘t’指日期/時間的轉換,’d’指一個月中的天數,在格式化時帶前導零的兩位數,即01 – 31;‘e’指格式化為兩位數,即1 – 31。

tl,tk格式小時數,’l’指12小時制(1 ~ 12),’k’指24小時制(0 ~ 23)。

tM格式分鐘數,’M’指格式化時帶前導零的兩位數,即00 ~ 59。

tS格式化秒,格式化時帶前導零的兩位數,即00 - 60("60"是支持閏秒所需的一個特殊值)。

tp特定于語言環境的上午或下午,標記以小寫形式表示,例如"am"或"pm"。

tDA date & time conversion—date as %tm%td%ty

088個字符寬度,寬度不足在頭部以“0”填充

+包含符號位(正數+或負數—)

,組分隔符

-左對齊(默認是右對齊)

.3保留3位小數

10.3表示10個字符寬度,保留三位小數(默認右對齊)

import java.util.Calendar;

import java.util.Locale;

public class IntegerText {

public static void main(String[] args) {

long n = 461012;

System.out.format("%d%n", n); // --> "461012"

System.out.format("%08d%n", n); // --> "00461012"

System.out.format("%+8d%n", n); // --> " +461012"

System.out.format("%,8d%n", n); // --> " 461,012"

System.out.format("%+,8d%n%n", n); // --> "+461,012"

double pi = Math.PI;

System.out.format("%f%n", pi); // --> "3.141593"

System.out.format("%.3f%n", pi); // --> "3.142"

System.out.format("%10.3f%n", pi); // --> " 3.142"

System.out.format("%-10.3f%n", pi); // --> "3.142"

System.out.format(Locale.FRANCE, "%-10.4f%n%n", pi); // --> "3,1416"

Calendar c = Calendar.getInstance();

System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006"

System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am"

System.out.format("%tD%n", c); // --> "05/29/06"

}

}

TheDecimalFormat Class

可以使用java.text.DecimalFormat格式化十進制數字的輸出,包括前/后導零、前后綴、組分隔符、小數點。盡管使用DecimalFormat可以靈活控制輸出的格式,另一面它也增加了代碼的復雜度。如下:通過模式patten構造DecimalFormat的對象myFormatter,接著調用format()方法(從NumberFormat繼承過來的),格式化輸出字符串。

import java.text.*;

public class DecimalFormatDemo {

static public void customFormat(String pattern, double value) {

DecimalFormat myFormatter = new DecimalFormat(pattern);

String output = myFormatter.format(value);

System.out.println(value + " " + pattern + " " + output);

}

static public void main(String[] args) {

customFormat("###,###.###", 123456.789); //-->123456.789 ###,###.### 123,456.789

customFormat("###.##", 123456.789); //-->123456.789 ###.## 123456.79

customFormat("00.000", 123.7876); //-->123.7876 00.000 123.788

customFormat("$###,###.###", 12345.67); //-->12345.67 $###,###.### $12,345

}

}DecimalFormat.java輸出說明

數值Pattern輸出說明

123456.789###,###.###123,456.789‘#’代表了一個阿拉伯數字,‘,’為組分隔符,‘.’用于分割整數和小數

123456.789###.##123456.79數值有三位小數,而模式里只用兩個,會通過四舍五入保留兩位小數

123.78000000.000000123.780‘0’代表了一個阿拉伯數字,位數不足需要在前或后一零補充,注意其與‘#’的區別。

12345.67$###,###.###$12,345.67以美元符號‘$’開頭,保留三位小數,且組分割為3

tips:組分隔符“,”通常用于千位,但是在某些國家/地區中回用于分隔萬位(如中國)。分組大小是分組字符之間的固定數字位數,例如100,000,000 是 3,而 1,0000,0000 則是 4。如果使用具有多個分組字符的模式,則最后一個分隔符和整數結尾之間的間隔才是使用的分組大小。所以 "#,##,###,####" == "######,####" =="##,####,####"。 即“123456.789”的模式為“###,#,##.###”,其輸出為“12,34,56.789”。

基本算數運算之外

java除了支持各種基本的算術運算(+, -, *, /, 和 %)之外,也為各種復雜的數學運算提供了工具類Math。通過Math類可以完成指數、對數、反三角函數等復雜的運算。

由于Math類的方法都是靜態的,你可以在類里直接調用Math類的方法,如下:

Math.cos(angle);tips:可以使用static import靜態導入Math類,如此便不用在每個方法下上Math;import static java.lang.Math.*;接著就可以直接在方法體里通過Math類的方法名調用Math類對應的方法:cos(angle);

常量與基本算術方法

在Math類里包含兩個常量:Math.E,自然底數e

Math.PI,圓周率

Math類包含了超過40個的靜態方法,為了分類來談,先列出Math里的基本算術方法:Math的基本算術方法

方法描述double abs(double d)

float abs(float f)

int abs(int i)

long abs(long lng)返回參數的絕對值

double ceil(double d)取得大于或等于參數的最小整數,并以double類型返回

double floor(double d)取得小于或等于參數的最大整數,并以double類型返回

double rint(double d)取得最接近參數的某一整數(類似于四舍五入),并以double類型返回

long round(double d)

int round(float f)返回最接近參數的long/int值。

double min(double arg1, double arg2)

float min(float arg1, float arg2)

int min(int arg1, int arg2)

long min(long arg1, long arg2)返回兩個數中較小的那一個

double max(double arg1, double arg2)

float max(float arg1, float arg2)

int max(int arg1, int arg2)

long max(long arg1, long arg2)返回兩個數中較大的那一個

BasicMathDemo演示了如何去使用Math的基本算術方法:public class BasicMathDemo {

public static void main(String[] args) {

double a = -191.635;

double b = 43.74;

int c = 16, d = 45;

System.out.printf("The absolute value " +

"of %.3f is %.3f%n",

a, Math.abs(a)); //-->The absolute value of -191.635 is 191.635

System.out.printf("The ceiling of " +

"%.2f is %.0f%n",

b, Math.ceil(b)); //-->The ceiling of 43.74 is 44

System.out.printf("The floor of " +

"%.2f is %.0f%n",

b, Math.floor(b)); //-->The floor of 43.74 is 43

System.out.printf("The rint of %.2f " +

"is %.0f%n",

b, Math.rint(b)); //-->The rint of 43.74 is 44

System.out.printf("The max of %d and " +

"%d is %d%n",

c, d, Math.max(c, d));//-->The max of 16 and 45 is 45

System.out.printf("The min of of %d " +

"and %d is %d%n",

c, d, Math.min(c, d));//-->The min of of 16 and 45 is 16

}

}

Math里指數與對數運算:

下表列出了在Math里關于指數與對數運算的方法:指數、對數運算的方法

方法描述

double exp(double d)返回自然底數e的d次方冪的值

double log(double d)返回d值的自然對數(底數是e)。類似的有log10(double a)

double pow(double base, double exponent)返回base的exponent次方的值

double sqrt(double d)返回d的算術平方根Demo:ExponentialDemopublic class ExponentialDemo {

public static void main(String[] args) {

double x = 11.635;

double y = 2.76;

System.out.printf("The value of " +

"e is %.4f%n",

Math.E); //-->The value of e is 2.7183

System.out.printf("exp(%.3f) " +

"is %.3f%n",

x, Math.exp(x));//-->exp(11.635) is 112983.831

System.out.printf("log(%.3f) is " +

"%.3f%n",

x, Math.log(x));//-->log(11.635) is 2.454

System.out.printf("pow(%.3f, %.3f) " +

"is %.3f%n",

x, y, Math.pow(x, y));//-->pow(11.635, 2.760) is 874.008

System.out.printf("sqrt(%.3f) is " +

"%.3f%n",

x, Math.sqrt(x));//-->sqrt(11.635) is 3.411

}

}

Math里三角函數運算

下表列出了Math里關于三角函數的方法,需要注意的是,這些方法的參數都是以弧度制來表示的——換句話說就是,你需要將角度轉換為弧度。可以使用方法toRadians(),將角度轉換為弧度。三角函數運算的方法

方法描述

double sin(double d)返回角的三角正弦值

double cos(double d)返回角的三角余弦值

double tan(double d)返回角的三角正切值

double asin(double d)返回一個值的反正弦值;返回的角度范圍在-pi/2到pi/2之間。

double acos(double d)返回一個值的反余弦值;返回的角度范圍在0.0到pi之間。

double atan(double d)返回一個值的反正切值;返回的角度范圍在-pi/2到pi/2之間。

double atan2(double y, double x)將矩形坐標(x,y)轉換成極坐標(r, theta),返回所得角theta值。

double toDegrees(double d)

double toRadians(double d)角度和與弧度的相互轉換方法

Demo:TrigonometricDemo,計算45度角的三角函數值:public class TrigonometricDemo {

public static void main(String[] args) {

double degrees = 45.0;

double radians = Math.toRadians(degrees);

System.out.format("The value of pi " +

"is %.4f%n",Math.PI);

//-->The value of pi is 3.1416

System.out.format("The sine of %.1f " +

"degrees is %.4f%n",

degrees,Math.sin(radians));

//-->The sine of 45.0 degrees is 0.7071

System.out.format("The cosine of %.1f " +

"degrees is %.4f%n",

degrees, Math.cos(radians));

//-->The cosine of 45.0 degrees is 0.7071

System.out.format("The tangent of %.1f " +

"degrees is %.4f%n",

degrees, Math.tan(radians));

//-->The tangent of 45.0 degrees is 1.0000

System.out.format("The arcsine of %.4f " +

"is %.4f degrees %n",

Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians))));

//-->The arcsine of 0.7071 is 45.0000 degrees

System.out.format("The arccosine of %.4f " +

"is %.4f degrees %n",

Math.cos(radians), Math.toDegrees(Math.acos(Math.cos(radians))));

//-->The arccosine of 0.7071 is 45.0000 degrees

System.out.format("The arctangent of %.4f " +

"is %.4f degrees %n",

Math.tan(radians), Math.toDegrees(Math.atan(Math.tan(radians))));

//-->The arctangent of 1.0000 is 45.0000 degrees

}

}

隨機數

Math類的random()方法可以產生大于等于0.0且小于1.0的(偽)隨機數(0 <=Math.random()<1),如此,你便能利用這個函數產生各中范圍的隨機數,如

要產生0~9的隨機數:int number = (int)(Math.random() * 10); //(0 <= number < 10)

如產生5~9的隨機數:int number = (int)(Math.random() * 5) + 5;

附:使用Math.random()可以產生一個隨機數。如果需要產生一系列隨機數,可以創建java.util.Random的對象,并調用對應的方法。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java 3number_java 数据Number、Math的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。