Implicit Conversions Between Numeric Types

Data of one type can be converted to another type either implicitly or explicitly.

This section describes some of the implications of the implicit conversions that are supported between different numeric data types. Explicit conversions can be carried out using the factory methods provided, as described in Supplemental Information.

When data of one numeric type is converted to another, there is not always any simple direct conversion, for example if you store a decimal value into a non-decimal field, or a BigInteger into an Integer.

In general:

  • If a decimal is stored into a non-decimal (such as BigDecimal stored into BigInteger), then the part of the data will be discarded. No rounding up is performed in this case: "9.99" is stored as "9".
  • If the maximum numeric size is exceeded (such as BigInteger stored into Integer) in such a way that the numeric value cannot be stored in the target type, then a NumberFormatException will be generated.

Support for assigning different numeric types

Data of one numeric type can be converted to another simply by assigning an object of one type to a value of a different type. In the following example, a BigDecimal item is converted to a BigInteger value.

A business process used by a bank contains a BOM class Balance, with two attributes:

  • accountBalanceinWholeNumbers  This is an integer, of sub-type Fixed Length (BigInteger), to provide an approximate value for the balance.
  • accountBalanceInDecimals  This is a decimal, of sub-type Fixed Point.

In the process, a data field balance is defined as an external reference to the BOM class Balance. The field is used in a script task, as shown in the following illustration.

An extract from the script is shown both in the illustration above and in the snippet that follows:

/*
Map the integer accountBalanceInWholeNumbers to the decimal accountBalanceInDecimals
*/
balance = com_example_accountconversion_Factory.createBalance();
balance.accountBalanceInDecimals = ScriptUtil.createBigDecimal("100.1");
balance.accountBalanceInWholeNumbers = balance.accountBalanceInDecimals;

BDS checks which data type is being passed in as input and performs the conversion accordingly. In this example, the code would convert the decimal 100.1 to the integer value 100.

Support for adding to Lists

BDS supports adding an item of one numeric type to a List object that is listing items of a different numeric type: for example, adding a Floating Point decimal to a list of Integers.

For example, if the variable approxWeight in the example below is an integer, and weightObservations is a list of these integers, you can add a decimal preciseWeight value to the list.

// Using the approxWeight variable already created, a new
// weight is added to the List weightObservations:
weightObservations.add(approxWeight);
...
// It is necessary to add to the list a more precise observation
// The fact that this is a decimal makes no difference to the
// script user:
weightObservations.add(preciseWeight);

So if the following observations are added to the list:

approxWeight = 200;
preciseWeight = 324.26;
approxWeight = 196

The weightObservations list would contain the values [200, 324, 196] after the script has run.

Note: As this example shows, some data may be lost when converting from decimal to integer.