Implicit type conversion: Difference between revisions

no edit summary
(Added C#)
No edit summary
Line 646:
The only two explicit conversion functions offered by Lua are <code>tonumber</code> and <code>tostring</code>.
Only the latter has a corresponding metamethod, so the former is usually only ever useful for converting strings, although in LuaJIT <code>tonumber</code> is used for converting numerical cdata into Lua numbers.
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Module Checkit {
Long a=12.5
\\ 12 is a double literal
Print a=12 ' compare a long with a double
Print a=12& ' compare two long values
def decimal b
b=12.e12
Print b=12000000000000 ' compare a decimal and a double
Print b=12000000000000@ ' now we compare two decimals
z=10#+20&
Print type$(z)="Currency"
\\ now z can't change type (except using a Swap)
z=100@ ' 100 decimal
Print type$(z)="Currency"
z=12.1234567 ' round to fit to currency
Print z=12.1235
\\ swap just swap values
swap z, b
Print type$(z)="Decimal", type$(b)="Currency"
\\ if a variable is a number then can be change to an object
z=(1,2,3,4,5)
Print type$(z)="mArray"
Try {
z=100
}
Print Error$ ' Missing Object
Function AnyType$(x) {
=type$(x)
}
Print AnyType$(z)="mArray"
Print AnyType$(100@)="Decimal", AnyType$(100#)="Currency"
\\ integer 16bit, long 32 bit
Print AnyType$(100%)="Integer", AnyType$(100&)="Long"
Print AnyType$(100~)="Single", AnyType$(100)="Double"
\\ double used as unsigned 32 bit long
Print AnyType$(0x100)="Double", AnyType$(0x100&)="Long", AnyType$(0x100%)="Integer"
Print 0xFFFFFFFF=4294967295, 0xFFFFFFFF&=-1, 0xFFFF%=-1
k=100@
Print AnyType$(val(k->Long))="Long"
\\ define type for argument
Function OnlyLong$(x as Long) {
=type$(x)
}
Print OnlyLong$(k)="Long"
Try {
s=stack:=1,2,3,4
Print OnlyLong$(s)
}
Print Error$ 'Wrong object type
Print AnyType$(s)="mStiva" ' name of object for stack
Try {
Print OnlyLong$(0xFFFFFFFF)
}
Print Error$ ' overflow long
}
Checkit
</lang>
 
=={{header|Oforth}}==
Anonymous user