I am not sure if I am using the right process here.I need to convert a double to a integer and I am getting a rounding error.
Dim cwp As Integer
… other code
canvisAR = (SWP / SHP)
canvisWP = (pjHeight * canvisAR)
cwp = CType(canvisWP, Integer)
When I run my code, CanvisWP is 1666.66666 and cwp ends up 1666 not 1667, what is the proper way to convert with normal rounding?
It helps if you read the documentation: http://documentation.xojo.com/index.php/CType
“CType does not necessarily preserve the value across the types. It converts the passed value source type to the destination data type. In that regard, it is identical to implicit conversion. If you are using CType to convert a real number data type (e.g., single, double, currency) to an integer data type, it truncates the value rather than rounds. Sign extension is preserved when converting from a signed data type to another signed data type. Conversion between signed and unsigned types (either way) preserves the bits but not the value.”
and the example therein:
The following examples illustrate loss of information in the conversion.
[code]Dim d As Double
Dim i As Integer
d = 566.75
i = CType(d, Integer) //returns 566[/code]