In this section, we’ll see some of the differences between legacy X++ and the new X++.

### Reals are Decimals

The type used to represent real values has changed from interpreted X++. This won’t require you to rewrite any code, because the new type can express all of the values that the old one could. We provide this material in the interest of full disclosure only.

All instances of the real type are now implemented as instances of the .NET decimal type (System.Decimal). Just as the real type in previous versions, the decimal type in a binary coded decimal type that, unlike floating point type, is resilient to rounding errors. The range and resolution of the decimal type are different from the original types.

The original X++ real type supported 16 digits and an exponent that defined where the decimal point is placed. The new X++ real type can represent decimal numbers ranging from positive

79,228,162,514,264,337,593,543,950,335 (296-1)

to negative

79,228,162,514,264,337,593,543,950,335 (-(296-1)).

The new real type doesn’t eliminate the need for rounding. For example, the following code produces a result of 0.9999999999999999999999999999 instead of 1. This is readily seen when using the debugger to show the value of the variables below.

public class MyClass2

{

public static void Main(Args a)

{

real dividend = 1.0;

real divisor = 3.0;

str stringvalue;

System.Decimal valueAsDecimal;

real value = dividend/divisor * divisor;

valueAsDecimal = value;

info(valueAsDecimal.ToString("G28"));

}

}

No number of decimals will suffice to represent the value of 1/3 accurately. The discrepancy obtained here is due to the fact that only a finite number of decimals are provided.

You should use the Round function to round to the number of decimals required.

You should use the Round function to round to the number of decimals required.

value = round(value, 2);

## No comments:

## Post a Comment