Type Conversion functions

These are the functions which are used to convert a data of one type into another.

SRON provides four functions for this:

  • to_bool()

  • to_double()

  • to_int()

  • to_string()

1. to_bool():

This function can convert values of type 'String', 'Int' and 'Double' to type 'Bool'.

{
    name : MAIN

    String str1 = "true"

    @ converting 'String' type to 'Bool'
     Bool val = to_bool(str1)
     println(val)

    @ converting 'Int' type to 'Bool', if the value is larger
    @ than zero than to_bool returns true otherwise false
     val = to_bool(-12)
     println(val)

    @ converting 'Double' type to 'Bool', it also work same 
    @ as double
    val = to_bool(123.122)
    println(val)
    
}

OUTPUT:

true false true


2. to_double():

This function can convert values of type 'String', 'Int' and 'Bool' to type 'Double'.

{
    name : MAIN

    String str1 = "-155.12425"

    @  converting 'String' type to 'Double'
     Double val = to_double(str1)
     println(val)

    @  converting 'Int' type to 'Double'
     val = to_double(970124123)
     println(val)

    @  converting 'Bool' type to 'Double'
     val = to_double(true)
     println(val)
    
}

OUTPUT:

-155.12425 970124123.0 1.0


3. to_int():

This function can convert values of type 'String', 'Double' and 'Bool' and 'Char' to type 'Int'.

{
    name : MAIN

    String str1 = "15512425"

    @  converting 'String' type to 'Int'
     Int val = to_int(str1)
     println(val)

    @  converting 'Double' type to 'Int'
     val = to_int(970124123.2344)
     println(val)

    @  converting 'Bool' type to 'Int'
     val = to_int(false)
     println(val)

    @  converting 'Char' type to 'Int'
     val = to_int('7')
     println(val)
    
}

OUTPUT:

15512425 970124123 0 7


4. to_string():

This function can convert values of any type to type 'String'.

{
    name : MAIN

    @  converting 'Int' type to 'String'
     String val = to_string(2663)
     println(val)

    @  converting 'Double' type to 'String'
     val = to_string(9701.133)
     println(val)

    @  converting 'Bool' type to 'String'
     val = to_string(true)
     println(val)

    @  converting 'Char' type to 'String'
     val = to_string('S')
    
}

OUTPUT:

2663 9701.133 true S [1,2,3,4,5]


Last updated