Reals
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
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.
Range and Precision
The range of reals is -(10)127 to (10)127 with a precision of 16 significant digits.
Declaring Reals
real declaration |
= |
real Variable { , Variable } ; |
variable |
= |
identifier [ option ] |
option |
= |
// 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[];
Decimal Literals
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++.
Exponential Notation
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
Automatic Conversions
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.
Conversion to .NET System.Decimal
Direct assignments between X++ real and .NET Framework System.Decimal convert the value correctly without the need to call any conversion function. This is demonstrated in the following code example.
static public void Main(Args _args)
{
real real9;
System.Decimal sysdec1;
// Direct assignments supported between these types.
sysdec1 = 2.3456;
real9 = sysdec1;
info(strFmt("strFmt says real9 == %1", real9));
}
/***
Message (05:48:43 pm)
strFmt says real9 == 2.35
***/
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
}
}
Overview of Reals
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 |
See also
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.