/ (division)

説明

四則演算の中の割り算を行う演算子 /(スラッシュ)です。
2 つの演算子を操作して結果を生成します。

構文

result = numerator / denominator;
result int、float、double、byte、short、long
分子 int、float、double、byte、short、long
分母 非ゼロの変数 int、float、double、byte、short、long

result: 変数。許容されるデータ型:int、float、double、byte、short、long。
分子:変数または定数。許可されたデータタイプ:int、float、double、byte、short、long。
分母:非ゼロの変数または定数。許可されたデータ型:int、float、double、byte、short、long。

コード例

int a = 50;
int b = 10;
int c = 0;
c = a / b;  // the variable 'c' gets a value of 5 after this statement is executed

注意

数値(オペランド)の一方が float 型または double 型の場合、浮動小数点演算が行われます。

オペランドが float/double 型で、結果を格納する変数が整数の場合は、整数部分のみが格納され、小数部分は失われます。

float a = 55.5;
float b = 6.6;
int c = 0;
c = a / b;  // the variable 'c' stores a value of 8 only as opposed to the expected result of 8.409
タイトルとURLをコピーしました