Exact Math

Functions for high precision arithmetic

Qualified Name Type

apoc.number.exact.add(stringA String, stringB String) - returns the sum 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 sum 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 sum of multiplying two given large numbers (using Java BigDecimal).

Function

apoc.number.exact.sub(stringA String, stringB String) - returns the sum 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 value of the given large number (using Java BigDecimal).

Function

apoc.number.exact.toInteger(string String, precision Integer, roundingMode String) - returns the integer value 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.