Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic type conversion in Visual Basic 6.0

Tags:

types

vb6

When we convert a float to integer in visual basic 6.0, how does it round off the fractional part? I am talkin about the automatic type conversion.

If we assign like

Dim i as Integer
i=5.5
msgbox i

What will it print? 5 or 6 ??

I was getting "5" a couple of months before. One day it started giving me 6! Any idea whats goin wrong? Did microsoft released some patches to fix something?

Update : 5.5 gets converted to 6 but 8.5 to 8 !

Update 2 : Adding CInt makes no difference. CInt(5.5) gives 6 and Cint(8.5) gives 8!! Kinda weired behaviour. I should try something like floor(x + 0.49);

like image 763
Tuxist Avatar asked Mar 15 '09 12:03

Tuxist


People also ask

What is type conversion in Visual Basic?

Type Conversions in Visual BasicThe process of changing a value from one data type to another type is called conversion. Conversions are either widening or narrowing, depending on the data capacities of the types involved. They are also implicit or explicit, depending on the syntax in the source code.

Which are the types of conversions in VB net?

Type Conversion Functions in VB.NETCBool(expression): It is used to convert an expression into a Boolean data type. CByte(expression): It is used to convert an expression to a Byte data type. CChar(expression): It is used to convert an expression to a Char data type.

Which type conversion is done by compiler automatically?

The implicit type conversion takes place when more than one data type is present in an expression. It is done by the compiler itself it is also called automatic type conversion.

What are the two types of data type conversions?

In computer science, type conversion or typecasting refers to changing an entity of one datatype into another. There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting.


1 Answers

Part of this is in the VB6 help: topic Type Conversion Functions. Unfortunately it's one of the topics that's not in the VB6 documentation on the MSDN website, but if you've installed the help with VB6, it will be there.

When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. CInt and CLng differ from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return a value of the same type as is passed in.

Implicit type coercion - a.k.a. "evil type coercion (PDF)" - from a floating point number to a whole number uses the same rounding rules as CInt and CLng. This behaviour doesn't seem to be documented anywhere in the manual.

If you want to round up when the fractional part is >= 0.5, and down otherwise, a simple way to do it is

 n = Int(x + 0.5)

And off the top of my head, here's my briefer version of Mike Spross's function which is a replacement for the VB6 Round function.

 'Function corrected, now it works. 
Public Function RoundNumber(ByVal value As Currency, Optional PlacesAfterDecimal As Integer = 0) As Currency
  Dim nMultiplier As Long
  nMultiplier = 10 ^ PlacesAfterDecimal
  RoundNumber = Fix(0.5 * Sgn(value) + value * nMultiplier) / CDbl(nMultiplier)
End Function

Sample output:

Debug.Print RoundNumber(1.6)       '2'
Debug.Print RoundNumber(-4.8)      '-5'
Debug.Print RoundNumber(101.7)     '102'
Debug.Print RoundNumber(12.535, 2) '12.54'
like image 69
MarkJ Avatar answered Sep 27 '22 22:09

MarkJ