为什么我不能隐式将double转换为int? [英] Why can't I implicitly convert a double to an int?

查看:149
本文介绍了为什么我不能隐式将double转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以将int隐式转换为double: double x = 5;

You can implicitly convert an int to a double: double x = 5;

您可以显式转换将int转换为double: double x =(double)5;

You can explicitly convert an int to a double: double x = (double) 5;

您可以将Double显式转换为int: int x =(int)5.0;

You can explicitly convert a double to an int: int x = (int) 5.0;

为什么不能隐式转换一个double int x = 5.0;

推荐答案

double 的范围大于 int 的范围。这就是为什么您需要显式强制转换。由于相同的原因,您不能将 long 隐式转换为 int

The range of double is wider than int. That's why you need explicit cast. Because of the same reason you can't implicitly cast from long to int:

long l = 234;
int x = l; // error

这篇关于为什么我不能隐式将double转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆