Re: [問題] for迴圈
※ 引述《winsonblue (人生有幾次瘋狂)》之銘言:
: 請問大家 我有一段code如下
: $step_length_m5=0.4;
: for($length_m5=0;$length_m5<=100;$length_m5+=$step_length_m5){
: ...}
: 照理說length_m5應該是0.4累加
: 可是有時候印出來的值卻會是 43.399999或44.799999之類的
: 為什麼會出現這種情形阿?
可以試著用 printf("%g\n", $length_m5); 會有以下的效果
96
96.4
96.8
97.2
97.6
或是 printf("%.1f\n", $length_m5);
96.0
96.4
96.8
97.2
97.6
請參考 http://perldoc.perl.org/functions/sprintf.html
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.64.233.30
討論串 (同標題文章)
Perl 近期熱門文章
PTT數位生活區 即時熱門文章