Android将double转换为int会导致Zero始终为零

时间:2014-01-19 03:44:10

标签: java android

我正在尝试将double值转换为int(现在不关心它是否舍入或丢弃小数)。

我尝试了很多方法并进行了搜索,我总是得到相同的结果,int是0。

for(double time=1;time<100; time++){
  Double sin = Double.valueOf(Math.sin(freq*time*2*Math.PI)/Math.exp(decay*time));
  BigDecimal sinBD = new BigDecimal(sin);

  int nowProgress = lastProgress* sinBD.intValue();
  Log.d("touch", "Time: " + time + " - BOUNCE PROGRESS: " + nowProgress + " SIN:" + sin + " " + sin.intValue()  );
  publishProgress(nowProgress);
}

输出是:

D/touch   (  405): Time: 92.0 - BOUNCE PROGRESS: 0 SIN:-5.650622119921064E-54 0
D/touch   (  405): Time: 93.0 - BOUNCE PROGRESS: 0 SIN:-2.9583499774959883E-54 0
D/touch   (  405): Time: 94.0 - BOUNCE PROGRESS: 0 SIN:2.9429214663931283E-55 0
D/touch   (  405): Time: 95.0 - BOUNCE PROGRESS: 0 SIN:-1.0777191865157576E-56 0
D/touch   (  405): Time: 96.0 - BOUNCE PROGRESS: 0 SIN:-4.775752566124532E-56 0
D/touch   (  405): Time: 97.0 - BOUNCE PROGRESS: 0 SIN:-3.367948939041307E-56 0
D/touch   (  405): Time: 98.0 - BOUNCE PROGRESS: 0 SIN:-1.8316705209724932E-56 0
D/touch   (  405): Time: 99.0 - BOUNCE PROGRESS: 0 SIN:2.577602055706179E-57 0

1 个答案:

答案 0 :(得分:3)

您的sin值非常小(非常接近0),因此当您将它们转换为int时,您将获得0

请注意,符号xxxE-yyxxx * Math.pow(10, -yy)

的另一种表示形式

修改

另外,请确保lastProgress不是0