index > Visual Basic for Applications (VBA) > Can you tell me why Int(1.4 * 100) = 139

Can you tell me why Int(1.4 * 100) = 139


Can someone help me to find solution:

Int(1.3 * 100) = 130

Int(1.4 * 100) = 139  (also  Int(1.38 to 1.49  * 100 ) )

Int(1.5 * 100) = 150

(VBA:Retail 6.0.874)

Thanks in advace

Grifi

Hello Grifi,

I don't know the reason but this should solve your problem

Chas

Sub test()
Dim a
Dim b
a = 1.38
b = 100
Debug.Print Int(a * b)

End Sub

ChasAA

That's brilliant.

It's to do with how computers store numbers. There is no binary equivalent to 1.4 only 1 (0001) or 2 (0010) so there is some process involved where the computer approximates a double (1.4) for storing it in memory. This generally works ok if you work like for like (although not always) but if you do some arithmetic and then convert to an integer bits get left off and in this case some funny results occur.

I'll see if I can find you a link that tells you more about it.




www.dsmyth.net/blog
Derek Smyth

I am not sure why, but after some testing. It is pretty interesting.

Anyway, to solve this issue, use CInt instead of Int. The Int() function seems to give this strange result, but CInt is totally fine. Also this problem only happends when you use Int() function. If you just do msgbox cstr(1.4 * 100), it will give you 140.

Try this code to see how things behave.

' explicit double
Dim a1 As Double, a2 As Double
a1 = 1.4
a2 = 100
MsgBox "CStr(a1 * a2) = " + CStr(a1 * a2) + vbNewLine + _
"CStr(Int(a1 * a2)) = " + CStr(Int(a1 * a2)) + vbNewLine + _
"CStr(CInt(a1 * a2)) = " + CStr(CInt(a1 * a2))

' implicit
Dim b1, b2
b1 = 1.4
b2 = 100
MsgBox "CStr(b1 * b2) = " + CStr(b1 * b2) + vbNewLine + _
"CStr(Int(b1 * b2)) = " + CStr(Int(b1 * b2)) + vbNewLine + _
"CStr(CInt(b1 * b2)) = " + CStr(CInt(b1 * b2))

' direct const
MsgBox "CStr(1.4 * 100) = " + CStr(1.4 * 100) + vbNewLine + _
"CStr(CInt(1.4 * 100)) = " + CStr(CInt(1.4 * 100)) + vbNewLine + _
"CStr(Int(1.4 * 100)) = " + CStr(Int(1.4 * 100)) + vbNewLine + _
"CStr(Int(CDbl(1.4 * 100))) = " + CStr(Int(CDbl(1.4 * 100)))

magicalclick
reply 4

You can use google to search for other answers

 

More Articles

• help on list box (Access)
• VB Script Crashes Access When Exporting to Excel
• Removing Hyperlinks
• Macro in Excel that will look at current worksheet column and sel...
• Emailing from an Access form.
• Argument syntax problem
• VBA resources
• I need help figuring out what is crashing my VBA for Access 2003
• My mini Utility_Move class, please have some suggestions.
• Accessing a drawing through AutoDesk Volo Viewer
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

• how can i copy my total from sheet 1 to
• Adding a worksheet to the workbook
• calling a function and passing the name
• Problem regarding VBA in excel - Combine
• passing a xml document from Excel to Ora
• copy chart from excel to word
• [Excel] How to convert Excel to jpg, gif
• Opening a Word file from a macro in Excel
• Textbox won't keep new values after work
• Macros - Excel
• Getting Excel Formulas to work in VBA
• Problem in Saving Excel 2003 Workbook in
• Form Show Problem
• Working with Outlook Public Folders from
• Program code fro sorting coloumns in a l

Hot Articles

• Initialize Sub routine
• Avoid message boxes during macro?
• insert column after pivot table
• Trim$ function in VB
• Table or Array in the header of an excel
• Can anybody explain this code for me ???
• Access Project and Views
• Method 'OnAction' of object 'CommandBarB
• VBA 6.4 now available on MSDN Subscription
• Using HTML Help in Microsoft Excel VBA
• Event handling for dynamically created c
• Retrieve IBM Workplace Forms data into E
• No Database Type
• create a chart based on QueryTable
• Someone mind helping me with Error 426

Recommend Articles

• Worksheet_Change Error
• VBA excel - problem with having clause i
• Table Caption Property
• Program aborts only when run with Access
• Binding a combo box item to a shape's cu
• VB6 XP Themes support. Solved! Source in
• Symbols for VBA6.DLL?!
• Entries to other forms
• I need to install ACTIVEX so I can be ab
• Left Joins with Multiple Conditions in VBA
• Excel Unprotect WorkSheet problem in Exc
• create a chart based on QueryTable
• How to match slide and query numbers
• AutoResize Merged Cells
• Applying InsertBreak to a style?