java 取小数的整数部分

时间:2008-06-05 13:55:38   来源:论坛整理  作者:  编辑:chinaitzhe
下面是一段代码
a=(Math.random());
pwd=(int)(a*10);
这样产生的整数后面老是有一个.0
例如9.0,5.0,6.0
有什么办法可以把后面那个0去掉吗?
网友回复:pwd声明成int型就成了

Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





        double a = Math.random();

        int pwd = (int)(a * 10);

        System.out.println(pwd);




网友回复:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



int intpwd = ((int) (Math.random() * 10));


网友回复:把PWD初始化成int型
网友回复:
引用 2 楼 anqini 的回复:
Java codeint intpwd = ((int) (Math.random() * 10));

网友回复:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





import java.util.Random;

...

Random rnd = new Random();

int pwd = rnd.nextInt(10);



关键字:java,小数,整数,部分,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面