.NET ライブラリには次のような関数があります
System.Math.Round(double, int)
しかし、それを機能させるために double 値を float にキャストする必要があるのはなぜですか..??
次のスクリーンショットを見てください。
.NET ライブラリには次のような関数があります
System.Math.Round(double, int)
しかし、それを機能させるために double 値を float にキャストする必要があるのはなぜですか..??
次のスクリーンショットを見てください。
次の機能
Math.Round(double value, int digits)
を返しますdouble
。次のコードを使用して、出力float
に名前のを定義しようとしました。ここで、は値の2倍であり、整数を表します。d
Math.Round(n,2)
n
1.12345
2
double n = 1.12345;
float d = Math.Round(n,2);
上記の関数からの出力はdouble
であり、ではないため、実際にはエラーが発生しますfloat
。
Cannot implictly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)
これを修正するには、次のように変更float d = Math.Round(n,2);
しますdouble d = Math.Round(n,2);
ありがとう、
これがお役に立てば幸いです:)
doubleからfloatに変換すると、精度が失われ、暗黙的に実行できなくなります。より正確なdouble変数にfloat値を割り当てると、コンパイラーは文句を言いません。