Exact Math

Functions for high precision arithmetic

Qualified Name Type

apoc.number.exact.add(stringA STRING, stringB STRING) - returns the result of adding the two given large numbers (using Java BigDecimal).

Function

apoc.number.exact.div(stringA STRING, stringB STRING, precision INTEGER, roundingMode STRING) - returns the result of dividing a given large number with another given large number (using Java BigDecimal).

Function

apoc.number.exact.mul(stringA STRING, stringB STRING, precision INTEGER, roundingMode STRING) - returns the result of multiplying two given large numbers (using Java BigDecimal).

Function

apoc.number.exact.sub(stringA STRING, stringB STRING) - returns the result of subtracting a given large number from another given large number (using Java BigDecimal).

Function

apoc.number.exact.toExact(number INTEGER) - returns the exact value of the given number (using Java BigDecimal).

Function

apoc.number.exact.toFloat(string STRING, precision INTEGER, roundingMode STRING) - returns the FLOAT of the given large number (using Java BigDecimal).

Function

apoc.number.exact.toInteger(string STRING, precision INTEGER, roundingMode STRING) - returns the INTEGER of the given large number (using Java BigDecimal).

Function

  • Possible roundingMode options are UP, DOWN, CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_EVEN, UNNECESSARY.

The precision parameter allows users to set the precision of the operation result. The default value of the precision parameter is 0 (unlimited precision arithmetic), while for roundingMode the default value is HALF_UP. For more information abouth the precision and roundingMode parameters, see Java’s MathContext page.

Examples

In the below example, the precision parameter is set to 2. Consequently, only the first two digits of the returned result are precise.

RETURN apoc.number.exact.div('5555.5555','5', 2, 'HALF_DOWN') as value;
Table 1. Results
Value

1100

In the below example, the precision parameter is set to 8. The first eight digits of the returned result are therefore precise.

RETURN apoc.number.exact.div('5555.5555','5', 8, 'HALF_DOWN') as value;
Table 2. Results
Value

1111.1111

These functions accept scientific notation as input. For example:

RETURN apoc.number.exact.add('1E6','1E6') as value;
Table 3. Results
Value

2000000

For more information, see Java’s BigDecimal page and Java’s BigInteger page.