java double tutorial with programming examples
このチュートリアルでは、プリミティブデータ型のJavaDoubleについて説明します。また、JavaBigDecimalやDecimalFormatクラスなどの関連クラスについても例を挙げて説明します。
このチュートリアルでは、構文とプログラミングの例を使用して、doubleデータ型について説明します。
ここでは、Javaの10進形式と大きな10進クラスについて、doubleデータ型を明確に理解するのに役立ついくつかのよくある質問とともに説明します。
学習内容:
Javaプリミティブ型
ご存知のとおり、Javaには、int、short、long、byte、float、double、char、booleanの8つのプリミティブ型があります。 Java doubleは、幅と範囲がfloatを超えるプリミティブデータ型の1つです。
プリミティブ型 | 幅(ビット) | 範囲 |
---|---|---|
ダブル | 64 | 4.9e-324から1.8e + 308 |
Javaダブル
Java doubleは、浮動小数点数を表すために使用されます。 64ビットを使用して変数値を格納し、float型よりも広い範囲を持ちます。
構文:
// square root variable is declared with a double type. double sqrt;
Javaの二重の例
この例では、長方形の面積の平方根を計算しています。長さと幅を整数とし、整数型の面積を計算しました。
平方根は10進値を与える可能性が最も高いため、変数Area_sqrtをdoubleとして宣言し、平方根を計算しました。
public class doubleExample { public static void main(String() args) { int length=15, breadth=25; int area; area = length*breadth; // calculating area of the rectangle System.out.println('Area of rectangle is ' + area); // declared a varibale which will store the square root double Area_sqrt; // calculating square root of Area of the rectangle Area_sqrt = Math.sqrt(area); System.out.println('Square root of area is ' +Area_sqrt); } }
出力
JavaDecimalFormat
Javaには、数値のフォーマットに使用されるDecimalFormatと呼ばれる特別なクラスがあります。このフォーマットはカスタマイズ可能です。
以下の例では、コンマ「、」で区切られたパターンと、double型の10進数を定義しています。このパターンまたはフォーマットを使用して、入力番号を表示します。
パターンをDecimalformatクラスに渡し、参照「df」を使用して出力をフォーマットしました。
import java.text.DecimalFormat; public class ExampleFormat { public static void main(String() args) { // defining a format in which number will be displayed String formatter = '##,###,###.##'; // initialized the decimal number double num = 12345678.12; // passed the pattern into the Decimal format class DecimalFormat df = new DecimalFormat(formatter); // printed the formatted number System.out.println('The formatted number is: ' +df.format(num)); } }
出力
Java BigDecimal
これも特別なJavaクラスであり、数値の単純な算術演算(加算、減算、乗算、除算)、結果の四捨五入、フォーマット変換などを提供します。
これをよりよく理解するために、以下の例を見てみましょう。
数値の四捨五入
パフォーマンステストの面接の質問と回答
以下の例では、10進数の単純な減算とBig-Decimalクラスによる減算の違いを示しています。
2つのdouble変数を初期化し、それらの値の差を計算しました。ここでも、同じ値のBig-Decimalクラスを使用して2つの変数を初期化し、それらの差を計算しました。
最後に、両方の値を印刷しました。それらの違いを確認できます。 Big Decimalの計算値は、自動的に四捨五入されます。
import java.math.BigDecimal; public class example { public static void main(String() args) { // Initialized two double numbers double length1 = 1.06; double breadth1 = 1.07; // Subtracting length and breadth double sub = breadth1-length1; System.out.println('Simple Subtraction = ' +sub); // Initialized two big decimal numbers with same value BigDecimal length2 = new BigDecimal('1.06'); BigDecimal breadth2 = new BigDecimal('1.07'); // Subtracting length and breadth length2 = breadth2.subtract(length2); System.out.println('Big Decimal Subtraction = ' + length2); } }
出力
よくある質問
Q#1)ダブルタイプは何バイトかかりますか?
回答: 8バイト。
Q#2)JavaのMathContextとは何ですか?
スタックデータ構造c ++
回答: MathContextは、数値の四捨五入モードと精度を指定するJavaのクラスです。不変オブジェクトを提供し、BigDecimalクラスによって実装される演算子に特定のルールを課すことも担当します。
ルールは次のとおりです。
RoundingMode.CEILING、
RoundingMode.DOWN、
RoundingMode.FLOOR、
RoundingMode.UP
以下の例では、double変数を初期化し、数字を丸めるさまざまなルールを設定しています。これは、渡した出力指定子に従って機能します。
例えば、 最初のprintステートメントでは、出力指定子として「3」を渡した天井関数を計算しています。これは、出力が3桁になることを意味します。同様に、最後のステートメントでは、「1」を渡したため、出力には1桁が含まれます。
import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; public class example { public static void main(String() args) { double d = 3.14; // Rounded off to the upper limit, the output will contain 3 digit System.out.println(new BigDecimal(d, new MathContext(3, RoundingMode.CEILING))); // Rounded off to the lower limit, the output will contain 3 digit System.out.println(new BigDecimal(d, new MathContext(3, RoundingMode.DOWN))); /* * Rounded off to the previous integer (discards the decimal value) * The output will contain 1 digit */ System.out.println(new BigDecimal(d, new MathContext(1, RoundingMode.FLOOR))); /* * Rounded off to the next integer (discards the decimal and increments integer) * The output will contain 1 digit */ System.out.println(new BigDecimal(d, new MathContext(1, RoundingMode.UP))); } }
出力
Q#3)Java Big Decimalは不変ですか?
回答: はい。 Big Decimalで特定の操作を実行するたびに、作成済みのオブジェクトを変更する代わりに、新しいオブジェクトが返されます。
Q#4)floatとdoubleの違いは何ですか?
回答: 以下に、floatとdoubleの違いを示します。
浮く | ダブル |
---|---|
単精度数を表します。 | 倍精度の数値を表します。 |
幅は32ビットで、範囲は1.4e–045〜3.4e +038です。 | 幅は64ビットで、範囲は4.9e–324〜1.8e +308です。 |
7桁です。 | 15〜16桁の数字が含まれます。 |
通貨換算操作に役立ちます。 | 戻り値の型がdoubleであるため、sin()、cos()、sqrt()で役立ちます。 |
倍精度より遅い。 | 長い数学演算を実行するように構築された最新のプロセッサでは、倍精度の方がはるかに高速です。 |
Q#5)数学のクラスとは何ですか?
回答: Mathクラスは、数学演算で使用されるすべてのメソッドを含むJavaのクラスです。 E(2.72)とpi(3.14)の2つの二重定数があります。
例えば、 三角法のsin()、cos()、tan()メソッド。指数関数のsqrt()、log()、pow()メソッド。 pow()のプログラミング例は、すでに上記で説明されています(Javaの二重の例)。
結論
このチュートリアルでは、適切な例を使用してダブルプリミティブ型について説明しました。また、プログラムにはDecimalFormatとBigDecimalJavaも含まれています。
よくある質問は、範囲、幅、サイズ、数学のクラスなど、double型のさまざまな領域にも含まれています。
このチュートリアルを完了すると、double型を詳細に理解できるようになり、算術演算に関する独自のロジックを作成する際にこれらの概念を使用できるようになります。
=> EasyJavaトレーニングシリーズをお読みください。