出来るのだASP Q&A掲示板(過去LOG)  訪問数 52046 昨日 889 今日 776
    【PR】 パソコン入門からIT専門書まで幅広く取り揃えています。セブン-イレブン受取り手数料無料のセブンアンドワイ。
Topに戻る 掲示板に戻る 検索 削除 管理者

[1832] Re:VBScript の Round関数にはバグがあります
投稿者:北岳さん 2002/03/08 9:02:12
【再チェック】
-------- roundx.asp --------
<html><body>
<%
Function roundx(X, Y)
z = X * 10 ^ Y
a = Int(X * 10 ^ Y)
If x>=0 and z - a >= 0.5 Then a = a + 1
if x<0 and z - a > 0.5 then a = a + 1
roundx = a / 10 ^ Y
End Function
'---
response.write "整数へ<br>"
for i=-9.5 to 9.5 step 1
response.write i & ","
next
response.write "<br>"
for i=-9.5 to 9.5 step 1
response.write roundx(i,0) & ","
next
'---
response.write "<p>小数へ<br>"
for i=9.044 to 9.056 step 0.001
response.write i & ","
next
response.write "<br>"
for i=9.044 to 9.056 step 0.001
response.write roundx(i,2) & " ,"
next
%>
</body></html>
--------------------------


【結果】
整数へ
-9.5,-8.5,-7.5,-6.5,-5.5,-4.5,-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,
-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9,10,

小数へ
9.044,9.045,9.046,9.047,9.048,9.049,9.05,9.051,9.052,9.053,9.05399999999999,9.05499999999999,9.05599999999999,
9.04 ,9.05 ,9.05 ,9.05 ,9.05 ,9.05 ,9.05 ,9.05 ,9.05 ,9.05 ,9.05 ,9.05 ,9.06 ,

9.055を四捨五入して9.06を得る筈のところが、9.05499999999999 に化けて9.05になってますね。
ややこしい・・・

VBScript の Round関数にはバグがあります   北岳さん [2002/03/08 7:00:32] [1831]
  Re:VBScript の Round関数にはバグがあります   北岳さん [2002/03/08 9:02:12] [1832]
  Re:VBScript の Round関数にはバグがあります   arikさん [2002/03/08 11:45:02] [1833]
    Re:VBScript の Round関数にはバグがあります   arikさん [2002/03/08 16:15:53] [1836]
  Re:VBScript の Round関数にはバグがあります   たまコさん [2002/03/08 12:25:47] [1834]
  VBScript の Round関数は使い方注意   北岳さん [2002/03/08 15:57:50] [1835]


TreeBBS For ASP V.0.1.3
Program By YasNet