String to Integer

How does one convert a String value to an Integer or convert a Double to an INTEGER type? For example, Val and Cdbl convert a string to a numerical value, returning the Double data type. But I cannot find any built-in functions that can convert a double to an integer or a string to an integer. Even the ROUND function evidently returns a double.

What does the Language Reference for ROUND, such as it is, mean when it says " …Although result is typed as a Double, it actually contains an integer value."
and … The result of this function is a Double, but it will always contain a whole number" ? Yes, I know that rounding, floor, ceiling, absolute values, etc. give you an “integer value” or a “whole number”, but what is the data type?

Just assign them.

dim d as double
dim s as string = str( d )

dim i as integer
s = str( i )

i = d // i will hold an integer
d = i // d will hold a double
i = val( s ) // i will be an integer

A Double converts automatically to an Integer. So you can write:

Dim s As String = "25" Dim i As Integer = Val(s)

Or you can use Text instead:

Dim t A Text = "25" Dim i As Integer = Integer.FromText(t)

Round only ever returns a value with no decimals. I suspect it uses a Double because when this method was first created a Double could hold higher values than an Integer.

An INTEGER can refer to a datatype (DIM I AS INTEGER), or a numeric value with nothing to the right of the decimal point
An INTEGER VALUE refers only to a numeric value with nothing to the right of the decimal point
So while you might have DIM d AS DOUBLE=3, it is a DOUBLE datatype that contains an INTEGER VALUE


Paul: I guess I’m a little surprised that a strongly-typed language like XOJO lets you get away with this. (And I’m relieved that I don’t have to always worry about these conversions). I must say that I’m struck by the number of Str() , Val(), Cdbl() functions I need to interconvert values from an app’s UI to code. Perhaps my extensive use of FileMaker in past years has spoiled me, since it, for the most part, is more forgiving.

Until you have to figure out why your result is “?”… :slight_smile:

(And yes, I’m a big FileMaker fan.)

XOJO is VERY forgiving (sometimes to a fault)… especially in comparision to Apples SWIFT… where you have to cast almost everything, including between differnt size Integers and Floats (not to mention they have many more datatypes to deal with)