Numeric Data Conversions

Numeric Operators

If the operator is one of: 

+, -, *, /, div, =, <, >, <=, >=, <>, mod, -(negation), shl, shr,

then ATEasy expects a numeric operand. It will convert the variables automatically to the most extended variable type. For example, if nX is a Short variable:

nA=2 ! Short variable

dB=12.8345 ! Double variable

nX=nA+dB ! Outcome is a Short variable

then nA is temporarily converted to a Double, added to dB, and the resultant 14.8345 is then converted to Short (truncated) to become 14. If the outcome had been declared as Double, the result would have been 14.8345 instead.

If you used a Boolean bC:

bC=True ! Boolean variable

nA=2 ! Short variable

nX=nA+bC

Then bC would be converted to numeric (-1) before the results are calculated and nX would become 1.

Note: When converting a signed numeric to a more extended numeric, the value is "sign extended" to retain the correct signed value.

Currency and DateTime Data Conversion to String Data Type

The numeric data value of types Currency and Datatime can be set to a string and vice versa. For example,

dtToday="9/20/2007"
dtCurTime="1:51:08 PM"
dtCurDateTime="12/26/2007 10:30:00 AM"
sDate=dtToday
sTime=dtCurTime
sNow=dtCurDateTime

curProfit="$30000"
s=curProfit
print s    ! this prints "$30,000.00"
 

Rules for Numeric Data Conversions

If either operand is of a more extended type, the other operand is converted to that type, from the most extended to the least extended, as shown in the table below:

 

Order of "most extended" to "least extended" data types

Currency

DateTime

Double

Float

DDWord

DLong

DWord

Long

WChar

Word

Short

Byte

Char

Note: if you attempt a numeric operator on a procedure or object variable, you will get a fatal error message, since these cannot be converted to true numeric values. Even though procedure and object variables are pointers (numeric) to the addresses of their contents, there is no meaning in performing numeric operations on them.

Type Promotion

Sometimes, intermediate calculation values can exceed their data type maximums. When this occurs, ATEasy will convert the data type to another data type or "promote" the result to a higher capacity data type, in order to complete the calculation. For example,

nA=20,000 ! Short variables nA, nB, nC, and nD

nB=30,000

nC=2

nD=(nA+nB)/nC

The intermediate value "nA + nB" exceeds the 32,767 for a Short variable. ATEasy will temporarily convert that value to Long in order to complete the calculation and will then convert the final result to the data type of nD (Short). If the final result still exceeds the maximum of nD (as would occur if nC=1), the result is truncated.

Conversion Functions

ATEasy provides the following conversion functions:

 

Functions

Description

Val

Converts a string value to a numeric.

Str

Converts a numeric value to a string.

See Also

Basic Data Types, String Data Types, DateTime Data Type