cancel
Showing results forΒ
Did you mean:Β

## How should I be rounding a number to 2 decimal places?

Graph Buddy

Hi I am currently trying to round an average, that looks like this:

``````MATCH (this)-[:HAS_RATING]-(r:Rating)
WITH avg(r.rating) AS ratingAverage
RETURN round(ratingAverage, 2)
``````

This, however, returns:

``````Too many parameters for function 'round' (line 3, column 20 (offset: 105))
"RETURN round(ratingAverage, 2)"
^
``````

Based on Neo4j docs this should be all I need, any ideas? Thank you in advance!

8 REPLIES 8
Graph Steward

You want to use:

built-in `round()` rounds to nearest int.

I also made a PULL REQUEST. I added missing documentation for the Rounding Modes.

Graph Buddy

"google is your friend" is what landed me here: Mathematical functions - numeric - Neo4j Cypher Manual

That not working is what landed me here: How should I be rounding a number to 2 decimal places?

Thanks for sharing the apoc library version.

Graph Steward

The APOC was in the second spot in Google. Google doesn't always get what you want in the first place!

Ninja

Hello @geronimo4j

Since Neo4j 4.2, your query is valid: Mathematical functions - numeric - Neo4j Cypher Manual

For previous versions, I was not using APOC:

``````MATCH (this)-[:HAS_RATING]-(r:Rating)
WITH avg(r.rating) AS ratingAverage
RETURN round(ratingAverage * 100) / 100 AS ratingAverage
``````

Regards,
Cobra

Graph Steward

argh!

Another take home lesson. Make sure you are looking at the latest version of the documentation!

4.2 doc shows all the variants of round that are available for that version:

4.1 doc doesn't show the new variants:

(4.1 doc probably should probably mention that 4.2 has more options.)

Google here, won't point you to the latest doc, unfortunately.

Node

Hello,
Sorry raw newbie here!
Does round work with AS in a query?

Node

The query above correctly returns my table, but i can't seem to find a way to reduce the remainder precision of the averages!
Any help greatly appreciated.

you could use apoc.number.format, for example

``````return apoc.number.format(1234.34343,'.##')
``````

which returns

``````1234.34
``````
Nodes 2022

NODES 2022, Neo4j Online Education Summit

OnΒ November 16 and 17 for 24 hours across all timezones, youβll learn about best practices for beginners and experts alike.

Neo4j Resources