Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Neo4j: Handling SUM's Scientific Notation

DZone's Guide to

Neo4j: Handling SUM's Scientific Notation

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

In some of the recent work I’ve been doing with neo4j the queries I’ve written have been summing up the values from multiple nodes and after a certain number is reached the value returned used scientific notation.

For example, in a cypher query like this:

START category = node:categories('category_id:1')

MATCH p = category-[:has_child*1..5]->subCategory-[:has_product]->product-[:sold]->sales

RETURN EXTRACT(n in NODES(p) : n.category_id?),subCategory.category_id, SUM(sales.sales)


I might get a result set like this:

+------------------------------------------------------------------------------------------------+
| EXTRACT(n in NODES(p) : n.category_id?)        | subCategory.category_id  | SUM(sales.sales)   |
+------------------------------------------------------------------------------------------------+
| ["246","254","255","3279",<null>,<null>]       | "3279"                   | 3.07213e07         |
| ["246","3649","3650","4362",<null>,<null>]     | "4362"                   | 1.023412e06        |
| ["246","287","291","308",<null>,<null>]        | "308"                    | 504712.5999448135  |
+------------------------------------------------------------------------------------------------+


I wanted to be able to add the first two rows together but still have them return separately which meant I needed to convert the values into decimal notation in order to do so.

I came across a Stack Overflow thread explaining how to do it in Ruby:

> "%f" % "3.07213e07"
=> "30721300.000000"

or

> "3.07213e07".to_f
=> 30721300.0

If we want to do the same thing in Java it’d read like this:

double d = Double.parseDouble("3.07213e07");
NumberFormat formatter = new DecimalFormat("###.#####");
String f = formatter.format(d);
System.out.println(f);
// returns 30721300

I couldn’t see a way to make the SUM function return in decimal notation but it’d be neat if there was a way to.

For now we have to apply some formatting on the result if we want to do any calculations with it.

 

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}