In VisualBasic I was able to do something like INT(j) to get the whole number (left of the decimal dropping everything to the right of the decimal). Is there a similar function/keyword in Xojo?

Example: j = 3.5, hence INT(j) = 3

In VisualBasic I was able to do something like INT(j) to get the whole number (left of the decimal dropping everything to the right of the decimal). Is there a similar function/keyword in Xojo?

Example: j = 3.5, hence INT(j) = 3

Try `floor`

for positive numbers, `ceil`

for negative numbers.

Also:

```
Function Int (d As Double) As Double
dim i as integer = d
return i
End Function
```

[quote=58159:@Kem Tekinay]Also:

```
Function Int (d As Double) As Double
dim i as integer = d
return i
End Function
```

[/quote]

You might want to make that an INT64? isn’t the magnitude of a DOUBLE larger than an “Integer”?

Good point:

```
Function Int (d As Double) As Double
dim i as Int64 = d
return i
End Function
```

Thank you. This works great.

Use floor in both cases, positive or negative numbers.

Ceil(-3.5) results in -3

Floor(-3.5) results in -4 just as in VB Int(-3.5) does

In VB Int() delivers the largest whole value that fits within the value where you take the int-function of, -4 fits within -3.5, but -3 doesn’t.

[quote=58163:@Kem Tekinay]Good point:

```
Function Int (d As Double) As Double
dim i as Int64 = d
return i
End Function
```

[/quote]

IMO this function gives the same result as the Fix() function in VB but not the Int() function in case of negative numbers.

Could be. I don’t use VB so I defer.

Could one perhaps use the integer division operation?

```
Function Int (d As Double) As Double
return d \\ 1
End Function
```

No idea which behaves better.

[quote=58180:@Andre Kuiper]In VB Int() delivers the largest whole value that fits within the value where you take the int-function of, -4 fits within -3.5, but -3 doesn’t.

[/quote]

He said “like Int() from VB” and followed with a description that was a little different ;-). I went with the description.

From the MSDN article

The difference between Int and Fix functions is that if Number is negative, Int returns the first negative integer less than or equal to Number, whereas Fix returns the first negative integer greater than or equal to Number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.

Forum for Xojo Programming Language and IDE. Copyright © 2024 Xojo, Inc.