Real variables can hold decimal values in addition to holding integers. For example, you might use a real to represent a currency amount. Internally, a real is stored as a Binary Coded Decimal (BCD encoding). The BCD encoding makes it possible to make exact representations of values that are multiples of 0.1.
The range of reals is -(10)127 to (10)127 with a precision of 16 significant digits.
// Simple declaration of a real variable, r
real r;
// Multiple declaration of two real variables
real r1, r2;
// A real variable is initialized to the approximate value of pi
real r3 = 3.1415;
// Declaration of a dynamic array of reals
real r4[];
You can use decimal literals anywhere where a real is expected. A decimal literal is the decimal written directly in the code, for instance 2.123876. The range of reals is shown above, and all reals in this range can be used as literals in X++.
Real literals can also be written using exponential notation. Examples are:
real r;
;
r = 1.000e3;
r = 1.2345e+3;
r = 1.2345e+03;
r = 1234.5e40;
r = 1.0e; // Means 1.0E1
X++ performs automatic conversion of reals to Booleans, enums, and integers in expressions, depending on the result of the expression.
If the result is an integer or the operator is an integer-operator, reals are converted into integers. If the result is a Boolean, reals are converted to Booleans, and so on. For example:
void main()
{
//Declares a variable of type integer with the name exprValue
int exprValue;
//Declares a real variable with the name area
real area = 3.141528;
;
exprValue = Area/3;
}
The expression Area/3 is a real expression because division is a real operator, and the result is 1.047176. This result is automatically converted (actually truncated) to an integer with the value 1, because exprValue is an integer.
Using Reals in Expressions
Reals can be used in all expressions and with both relational operators and arithmetic operators as shown in the following example:
void myMethod()
{
// Two real variables are declared and initialized
real i = 2.5, j = 2.5;
;
// j is assigned the result of j * i, so j=6.25
j = j * i;
if (j > (i * 2)) // If j > 5
{
print "Great"; // "Great" is printed
}
else
{
print "Oops"; // else "Oops" is printed
}
}
|
Keyword
|
real
|
|
Size of data type
|
BCD-encoding: 64 bit
|
|
Scope of data type
|
] -(10)127 ; (10)127 [, with a precision of 16 significant digits
|
|
Default value
|
0.00
|
|
Implicit conversions
|
Automatically converted to boolean, enum, and int
|
|
Explicit conversions
|
str2num, num2str
|