1

I tried to round off value using VBscript

here is my code

sql = "select lastcode from " & session("gasbook") & ".acccode where account='" _
      & Trim(rsgrn.fields("stockcode")) & "'"
response.write sql
rs.Open sql, cn
if err.number<>0 then
    cn.rollbacktrans
    call HandleError(err.number,err.description,err.source)
        Response.End
else
    if (rs.EOF and rs.BOF) then
        cn.RollbackTrans
        Response.Write "GAS Code doesn't exist in " & session("gasbook")
        Response.End
    else
        If clng(rs.Fields("lastcode")) <> 0 Then
            cn.RollbackTrans
            Response.Write "Stock Account must be control account in " _
              & session ("gasbook")
            Response.End
        End If
    end if
end if
dim tmp
tmp = rsgrn.Fields("amount")
response.write tmp
response.write round(tmp)

tmp has value 2984.5, but when I apply round on tmp it convert into 2984 instead of 2984.5.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

2 Answers2

0

When in doubt, read the documentation. Without the optional second parameter numdecimalplaces, the Round function returns an integer:

numdecimalplaces

Optional. Number indicating how many places to the right of the decimal are included in the rounding. If omitted, integers are returned by the Round function.

Change the line

response.write round(tmp)

to

response.write round(tmp, 1)

if you want the value rounded to one decimal.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

The number is rounded to 2948 because the .Net framework follows the Bankers Rounding algorithm off of the IEEE 754 standard. It is not a question of if the number was rounded, it is rounded to the nearest even number due to standards.

Evidence: Here. and Here.

So, the round function is working perfectly to the IEEE Standards.

Community
  • 1
  • 1
Rich
  • 4,134
  • 3
  • 26
  • 45