java中的數值計算應該注意的問題
在使用數值計算的時候,應該小心,看看下邊這段代碼的結果。當使用float和double是要謹慎
import
?java.math.BigDecimal;?


public
?
class
?TestNum?
{

?
public
?
static
?
void
?main(String[]?args)
{
???????
double
?s
=
0
;
????
for
?(
int
?i
=
0
;?i
<
26
;?i
++
)
??????s?
+=
?
0.1
;
????System.out.println(s);?

????
double
?d?
=
?
29.0
?
*
?
0.01
;
????System.out.println(d);
????System.out.println((
int
)?(d?
*
?
100
));
????
????BigDecimal?num
=
new
?BigDecimal(
0.01
);
????
double
?dnum
=
num.doubleValue();
????System.out.println(dnum);
?}
}
?






















posted on 2006-09-20 22:14 dreamstone 閱讀(411) 評論(0) 編輯 收藏 所屬分類: jdk相關