# Reals

**Dynamics AX 2009**

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.

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 } }