locked
VBScript Not Returning Correct Results RRS feed

  • Question

  • When executing the following VB script, incorrect results are returned. The first message box says the correct number, 6, while the second message box returns an incorrect number, 5. Any ideas why this is happening?

    MonthsToPay = 651.06/108.51

    MSGBOX("Number after dividing: " & MonthsToPay)

    MSGBOX("Integer part of that number: " & INT(MonthsToPay))

    Monday, May 9, 2016 7:48 PM

Answers

All replies

  • That's why I always use Round to 0 decimal places instead of Int or Fix :

    MSGBOX("Integer part of that number: " & Round(MonthsToPay,0))
    


    Evgenij Smirnov

    msg services ag, Berlin -> http://www.msg-services.de

    my personal blog (mostly German) -> http://it-pro-berlin.de

    Windows Server User Group, Berlin -> http://www.winsvr-berlin.de

    Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com

    Monday, May 9, 2016 9:06 PM
  • That would solve the very specific question I asked, however, in the real application the numbers are variable. For example, in the below case, your method would yield a 7, which is not the integer part of 6.64. Any ideas to get around that?

    MonthsToPay = 721.06/108.51
    
    MSGBOX("Number after dividing: " & MonthsToPay)
    
    MSGBOX("Integer part of that number: " & Round(MonthsToPay,0))
    

    Tuesday, May 10, 2016 12:08 PM
  • OK, you can combine both approaches, this should yield the expected result every time:

    MSGBOX("Integer part of that number: " & Fix(Round(MonthsToPay,2)))


    Evgenij Smirnov

    msg services ag, Berlin -> http://www.msg-services.de

    my personal blog (mostly German) -> http://it-pro-berlin.de

    Windows Server User Group, Berlin -> http://www.winsvr-berlin.de

    Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com

    • Marked as answer by DB.Dave Tuesday, May 10, 2016 12:19 PM
    Tuesday, May 10, 2016 12:15 PM