cancel
Showing results for 
Search instead for 
Did you mean: 

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

geronimo4j
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

clem
Graph Steward

You want to use:

built-in round() rounds to nearest int.

(google is your friend!)

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

"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.

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

Cobra
Ninja
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

clem
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.

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

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
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.