0

I stored some numbers on Neo4j in String type. But now, But now I want to convert this number to double.I searched and it looks like there are only this functions: toFloat(), toInt() and toString(). But the function toFloat() don't have the precision that I need, double or numeric are the indicated. Some ideas?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jessica
  • 227
  • 3
  • 12

1 Answers1

2

The conversion functions are named inaccurately.

In Cypher, and Neo4j internally, only larger data types are used. toInteger() returns Longs, and toFloat() returns Doubles.

Also, from a previous answer to a similar question about double precision:

double only has 15/16 digits of accuracy and when you give it a number it can't represent (which is most of the time, even 0.1 is not accurate) it takes the closest representable number.

Tomas Vancoillie
  • 3,463
  • 2
  • 29
  • 45
InverseFalcon
  • 29,576
  • 4
  • 38
  • 51
  • the string is "43.988399999999999" when I use toFloat() this returns only 43.9884 – Jessica Feb 04 '17 at 15:20
  • You get the exact same results in Java when you use `Double.parseDouble("43.988399999999999")` – InverseFalcon Feb 04 '17 at 17:04
  • I'm using python, like this: result = session.run("MATCH (s:Sensor {IDPostgres: '568'})-[r]-(d:Data)-[r1]-(h:Hora)-[r2]-(p:Precipitacao) RETURN max(toFloat(p.valor));") – Jessica Feb 04 '17 at 21:04
  • The neo4j server is written in Java. – cybersam Feb 04 '17 at 21:42
  • Hmmm, but I need the number without rounding. Any idea? – Jessica Feb 05 '17 at 19:10
  • Java's Double type doesn't seem like it can capture the precision you need for that value. BigDecimal should be able to, but this isn't how Neo4j stores these values, and I don't think you can force it to use BigDecimal instead. If Python has the precision to represent this value, you might consider getting the :Precipitacao node with the max float value, but return the string value to Python and parse it into whichever type can represent the precision you need. Or, if you just need the string representation (which is already o the node), return the string value of the node with the max value. – InverseFalcon Feb 05 '17 at 19:38
  • You also want to make sure you actually need this precision. You're asking about precision down to the quadrillionth of whatever unit you're using. If we're talking about measuring precipitation, it seems like a stretch that this level of precision would be useful. – InverseFalcon Feb 05 '17 at 19:44