double 类型的数字怎么转换成int型呢?

Javafan3 2003-04-14 12:02:09
谢谢
...全文
3997 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongdongshu 2003-04-14
  • 打赏
  • 举报
回复
学名叫“主类型的过载”。
徐蕴 2003-04-14
  • 打赏
  • 举报
回复
考虑到你或者需要四舍五入或者截尾,可以使用floor, ceil 或者round函数然后用(int)强制转换,但是你应该考虑到可能会丢失信息的问题
dongdongshu 2003-04-14
  • 打赏
  • 举报
回复
JAVA提供的强制类型转换。
正如上面所说:
double d = 12.0;
int i = (int)d;
不过,会丢失一部分信息。
jczjczjcz 2003-04-14
  • 打赏
  • 举报
回复
double d = 12.0;
int i = (int)d;
gaojunbo 2003-04-14
  • 打赏
  • 举报
回复
double d = 12.0;
int i = (int)d;
moumouren 2003-04-14
  • 打赏
  • 举报
回复
double d = 12.0;
int i = (new Double(d)).intValue();
Javafan3 2003-04-14
  • 打赏
  • 举报
回复
能给原代码吗?
acefr 2003-04-14
  • 打赏
  • 举报
回复
intValue
public int intValue()
Returns the value of this Double as an int (by casting to type int).

Specified by:
intValue in class Number
Returns:
the double value represented by this object converted to type int
acefr 2003-04-14
  • 打赏
  • 举报
回复
intValue()
geshizan 2003-04-14
  • 打赏
  • 举报
回复
up
tangmi 2003-04-14
  • 打赏
  • 举报
回复
up,
参照以上意见,自己写几个试试就行了:)
MyXin 2003-04-14
  • 打赏
  • 举报
回复
呵呵!Double.intValue()就是执行了这样一句(int)value ;(value为double值,就是Double的double值)。呵呵!
MyXin 2003-04-14
  • 打赏
  • 举报
回复
是说我吗?
MyXin 2003-04-14
  • 打赏
  • 举报
回复
用int强制转换,不会有舍入,整数部分是几就是几。有时候还能方便,如取小数位:

double test = 15.0789;
int tmp = (int)test;
double tail = test-tmp;

呵呵!
lkenshin 2003-04-14
  • 打赏
  • 举报
回复
兄弟,还是多看看java的api吧,上面都有
MyXin 2003-04-14
  • 打赏
  • 举报
回复
如果只是想取得整数部分,就用(int)强制转换最好。
Weblog 2003-04-14
  • 打赏
  • 举报
回复
同意 moumouren(某某人)

不要使用(int)来强制转换

应该先生成一个double的外覆型Double,再用Double的intValue()得到int

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧