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

Coding Questions, IDE related or anything else

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

Postby tanzanite » Sun Apr 11, 2010 12:44 am

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.
tanzanite
 
Posts: 9
Joined: Thu Apr 01, 2010 5:46 am
Location: Mexico

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

Postby berndnoetscher » Mon Apr 12, 2010 10:14 am

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


berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

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

Postby tanzanite » Tue Apr 13, 2010 1:26 am

I think it is a good idea. It would aid in converting software and also make a simple way to do type conversions.
tanzanite
 
Posts: 9
Joined: Thu Apr 01, 2010 5:46 am
Location: Mexico


Return to Objective-Basic (Xcode 3)

Who is online

Users browsing this forum: No registered users and 1 guest