Page 1 of 1

Type conversion like cint(), val(), etc.

PostPosted: Sun Apr 11, 2010 12:44 am
by tanzanite
I am having trouble with type conversion in Objective-basic. I see no functions equivalent to these, commonly used in REALbasic:
val(), cint() - used to convert text to integer or numeric values
cstr() - used to convert numeric values to strings.

How do you accomplish these simple tasks in Objective-basic? I see it is sometimes possible to force things into id variables and use those, but it doesn't seem to always work. Ways of doing this are quite important to the applications I need to write as I must do math and extract numeric values from text strings, then convert these results to numbers again.

Re: Type conversion like cint(), val(), etc.

PostPosted: Mon Apr 12, 2010 10:14 am
by berndnoetscher
This following example shows how to do conversion between basic types like string, double and integer.

I am thinking of having a compatibility class for realbasic features like rb.cint() or rb.val(). What do you think of it?

Code: Select all
' This example shows how to do conversion between basic types like string, double and integer
' text to integer or numeric values

Event AwakeFromNib()
 
' similar to cstr()
' #1 convert number (integer, double...) to string  using the format functions or the String(...) cast
   
    ' convert an integer to a string
    Dim i As Integer = 99
    Dim s As String = String(i)
    Alert(s, ClassName(s))
    i = 88
    s = Format(88)
    Alert(s, ClassName(s))
   
    ' convert a double to a string
    Dim d As Double = 12.45
    s = FormatDecimal(d)
    Alert(s, ClassName(s))
    d = 33.7789
    s = FormatFloating(d)
    Alert(s, ClassName(s))
 
' similar to cint() or val()
' #2 convert string to number (integer, double...) Integer(...) cast or Declare functions to directly use Cocoa

    ' convert a string to an integer
    s = "1234"
    i = Integer(s)
    Alert(i, ClassName(i))
    i = s.integerValue()
    Alert(i, ClassName(i))
   
    ' convert a string to a double
    s = "1234.564"
    d = s.doubleValue()           ' Double(s) is not implemented yet
    Alert(d, ClassName(d))
 
End Event

Declare Function "String"- (NSInteger)integerValue
Declare Function "String"- (double) doubleValue



Re: Type conversion like cint(), val(), etc.

PostPosted: Tue Apr 13, 2010 1:26 am
by tanzanite
I think it is a good idea. It would aid in converting software and also make a simple way to do type conversions.