Mathematical functions - numeric
Numeric mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also Mathematical operators.
Example graph
The following graph is used for the examples below:
To recreate the graph, run the following query against an empty Neo4j database:
CREATE
(alice:Developer {name:'Alice', age: 38, eyes: 'Brown'}),
(bob:Administrator {name: 'Bob', age: 25, eyes: 'Blue'}),
(charlie:Administrator {name: 'Charlie', age: 53, eyes: 'Green'}),
(daniel:Adminstrator {name: 'Daniel', age: 54, eyes: 'Brown'}),
(eskil:Designer {name: 'Eskil', age: 41, eyes: 'blue', likedColors: ['Pink', 'Yellow', 'Black']}),
(alice)-[:KNOWS]->(bob),
(alice)-[:KNOWS]->(charlie),
(bob)-[:KNOWS]->(daniel),
(charlie)-[:KNOWS]->(daniel),
(bob)-[:MARRIED]->(eskil)
abs()
Syntax |
|
||
Description |
Returns the absolute value of an |
||
Arguments |
Name |
Type |
Description |
|
|
A numeric value from which the absolute number will be returned. |
|
Returns |
|
|
If |
MATCH (a), (e) WHERE a.name = 'Alice' AND e.name = 'Eskil'
RETURN a.age, e.age, abs(a.age - e.age)
The absolute value of the age difference is returned.
a.age | e.age | abs(a.age - e.age) |
---|---|---|
|
|
|
Rows: 1 |
ceil()
Syntax |
|
||
Description |
Returns the smallest |
||
Arguments |
Name |
Type |
Description |
|
|
A value to be rounded to the nearest higher integer. |
|
Returns |
|
|
RETURN ceil(0.1)
The ceil of 0.1
is returned.
ceil(0.1) |
---|
|
Rows: 1 |
floor()
Syntax |
|
||
Description |
Returns the largest |
||
Arguments |
Name |
Type |
Description |
|
|
A value to be rounded to the nearest lower integer. |
|
Returns |
|
|
RETURN floor(0.9)
The floor of 0.9
is returned.
floor(0.9) |
---|
|
Rows: 1 |
isNaN()
Syntax |
|
||
Description |
Returns whether the given |
||
Arguments |
Name |
Type |
Description |
|
|
A numeric value to be compared against |
|
Returns |
|
|
RETURN isNaN(0/0.0)
true
is returned since the value is NaN
.
isNaN(0/0.0) |
---|
|
Rows: 1 |
rand()
Syntax |
|
||
Description |
Returns a random |
||
Returns |
|
RETURN rand()
A random number is returned.
rand() |
---|
|
Rows: 1 |
round()
Syntax |
|
||
Description |
Returns the value of a rounded number, optionally using a specified precision and rounding mode. |
||
Arguments |
Name |
Type |
Description |
|
|
A value to be rounded. |
|
|
|
The rounding precision. |
|
|
|
A precision rounding mode ( |
|
Returns |
|
mode |
Description |
---|---|
|
Round away from zero. |
|
Round towards zero. |
|
Round towards positive infinity. |
|
Round towards negative infinity. |
|
Round towards closest value of given precision, with ties always being rounded away from zero. |
|
Round towards closest value of given precision, with ties always being rounded towards zero. |
|
Round towards closest value of given precision, with ties always being rounded to the even neighbor. |
For the rounding modes, a tie means that the two closest values of the given precision are at the same distance from the given value. E.g. for precision 1, 2.15 is a tie as it has equal distance to 2.1 and 2.2, while 2.151 is not a tie, as it is closer to 2.2. |
|
RETURN round(3.141592)
3.0
is returned.
round(3.141592) |
---|
|
Rows: 1 |
RETURN round(-1.5)
Ties are rounded towards positive infinity, therefore -1.0
is returned.
round(-1.5) |
---|
|
Rows: 1 |
round() with precision
RETURN round(3.141592, 3)
3.142
is returned.
round(3.141592, 3) |
---|
|
Rows: 1 |
RETURN round(-1.5, 0)
To align with round(-1.5)
, -1.0
is returned.
round(-1.5, 0) |
---|
|
Rows: 1 |
RETURN round(-1.55, 1)
The default is to round away from zero when there is a tie, therefore -1.6
is returned.
round(-1.55, 1) |
---|
|
Rows: 1 |
round() with precision and rounding mode
RETURN round(1.249, 1, 'UP') AS positive,
round(-1.251, 1, 'UP') AS negative,
round(1.25, 1, 'UP') AS positiveTie,
round(-1.35, 1, 'UP') AS negativeTie
The rounded values using precision 1 and rounding mode UP
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'DOWN') AS positive,
round(-1.251, 1, 'DOWN') AS negative,
round(1.25, 1, 'DOWN') AS positiveTie,
round(-1.35, 1, 'DOWN') AS negativeTie
The rounded values using precision 1 and rounding mode DOWN
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'CEILING') AS positive,
round(-1.251, 1, 'CEILING') AS negative,
round(1.25, 1, 'CEILING') AS positiveTie,
round(-1.35, 1, 'CEILING') AS negativeTie
The rounded values using precision 1 and rounding mode CEILING
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'FLOOR') AS positive,
round(-1.251, 1, 'FLOOR') AS negative,
round(1.25, 1, 'FLOOR') AS positiveTie,
round(-1.35, 1, 'FLOOR') AS negativeTie
The rounded values using precision 1 and rounding mode FLOOR
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'HALF_UP') AS positive,
round(-1.251, 1, 'HALF_UP') AS negative,
round(1.25, 1, 'HALF_UP') AS positiveTie,
round(-1.35, 1, 'HALF_UP') AS negativeTie
The rounded values using precision 1 and rounding mode HALF_UP
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'HALF_DOWN') AS positive,
round(-1.251, 1, 'HALF_DOWN') AS negative,
round(1.25, 1, 'HALF_DOWN') AS positiveTie,
round(-1.35, 1, 'HALF_DOWN') AS negativeTie
The rounded values using precision 1 and rounding mode HALF_DOWN
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'HALF_EVEN') AS positive,
round(-1.251, 1, 'HALF_EVEN') AS negative,
round(1.25, 1, 'HALF_EVEN') AS positiveTie,
round(-1.35, 1, 'HALF_EVEN') AS negativeTie
The rounded values using precision 1 and rounding mode HALF_EVEN
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
sign()
Syntax |
|
||
Description |
Returns the signum of an |
||
Arguments |
Name |
Type |
Description |
|
|
A positive or negative number. |
|
Returns |
|
|
RETURN sign(-17), sign(0.1)
The signs of -17
and 0.1
are returned.
sign(-17) | sign(0.1) |
---|---|
|
|
Rows: 1 |