Reals [AX 2012]
Updated: April 17, 2012
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.
|
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[];
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
***/
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
}
}
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.