# Decimal Numbers

Inaportuses Decimal variable types to allow the user to control the precision and scale of Numbers used in operations carried out on the import data. Decimals are arbitrary-precision signed decimal numbers. A Decimal consists of an arbitrary precision integer value and a non-negative integer scale, which represents the number of decimal digits to the right of the decimal point. Inaport provides functions for basic arithmetic, scale manipulation and format conversion of Decimals.

Decimals give the user complete control over rounding behavior, forcing explicit specification of rounding behavior for operations capable of discarding precision (divdecimal() and decimalsetscale()). Four rounding modes are provided for this purpose. The rounding modes are as follows:

### Rounding modes:

int | String | Corresponding roundingMode
1 | "rup" | Decimal.ROUND_UP Always increment the digit prior to a non-zero discarded fraction. Note that this rounding mode never decreases the magnitude. (Rounds away from zero.)
2 | "rdown" | Decimal.ROUND_DOWN Never increment the digit prior to a discarded fraction (i.e., truncate). Note that this rounding mode never increases the magnitude. (Rounds towards zero.)
3 | "rhalfup" | Decimal.ROUND_HALF_UP Behave as for ROUND_UP if the discarded fraction is >= .5; otherwise, behave as for ROUND_DOWN. (Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case rounds up.)
4 | "rhalfdown" | Decimal.ROUND_HALF_DOWN Behave as for ROUND_UP if the discarded fraction is > .5; otherwise, behave as for ROUND_DOWN. (Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case rounds down.)

The decimalsetscale() function is provided for manipulating the scale of a Decimal. It returns a Decimal whose value is approximately (or exactly) equal to that of the operand, but whose scale is the specified value; that is, they increase or decrease the precision of the number with minimal effect on its value. Significant care should be taken when using Decimals that the scale (i.e. the number of decimal places) has been set correctly, otherwise calculations using Decimals with large numbers of decimal places may result, give unexpected or inaccurate results. It is important to note that, for example, a Decimal generated from a number with two decimal places will not automatically have a scale of two itself, but should have its scale set appropriately before being used in further operations. See decimalsetscale() for more information on setting scale.

Conversion to and from Decimal values is handled using appropriate functions - decimaltodouble(), decimaltostring(), stringtodecimal() and numbertodecimal() allow straightforward conversion between Decimal, Number and String types.

## See also

adddecimal(), subdecimal(), muldecimal(), divdecimal(), decimalsetscale(), decimaltodouble(), numbertodecimal(), stringtodecimal()