# Cypher: Integer division should maybe result in Float, if appropriate? #493

Closed
opened this Issue Feb 4, 2013 · 17 comments

Projects
None yet
7 participants
Contributor

### freeeve commented Feb 4, 2013

 What do you guys think? I was kind of hoping this would give me 0.5: http://console.neo4j.org/?id=aoipbr Otherwise, maybe we need some way of casting to a float after all? 1/2.0 works as expected, at least.
Contributor

### technige commented Feb 4, 2013

 +1 for implementing "true division". This exact same issue was a big talking point with Python some years back and one of the notable changes in Python 3. Full details of the change are here: http://www.python.org/dev/peps/pep-0238/. tl;dr Python 3 now supports two division operators - "/" for true division and "//" for floor division.
Contributor

### freeeve commented May 23, 2013

 Just ran into this again. Any fundamental reasons why this shouldn't be like this? I can submit a pull request.
Member

### jexp commented May 23, 2013

 I'd love it. If you find time @wfreeman that would be great. As `//` is already used for comments, we should probably use `/` for floor division and `./` or `/.` for real division ?

 +1

### MatAtBread commented Feb 10, 2014

 I managed to "avoid" this case by divding my integer field values by literal floats: "c.value/2.0" which yields a float. Won't work (I guess) if the case is "c.numer/c.denom" where both are ints. Apart from str(), Cypher (neo?) is too dumb when it comes to type conversion. As well as str(), there should be a way to do conversion to floats (e.g. number(c.stringValue)), and from there floor(), ceil(), round(). Returning null from number() would be a good way of indicating un-parsable strings: "where number(str) is not null"
Contributor

### cleishm commented Feb 10, 2014

 We'll certainly consider adding support for explicit conversion in future. Right now, you can always force a conversion by doing something like `(c.numer*1.0)/c.denom`.

### MatAtBread commented Feb 10, 2014

 Fabulous. Any workarounds for converting strings to numbers?
Contributor

### cleishm commented Feb 10, 2014

 No: that's non-trivial unfortunately
Contributor

### freeeve commented Feb 10, 2014

 Make them numbers from the start. Convert them to strings when needed as strings.

### MatAtBread commented Feb 10, 2014

 Sadly that's not always so easy when the original source of the data isn't on spec, and the dB has no type enforcement. Ideally, it wouldn't happen, but when it does, running a query on the DB to fix it is preferable to writing some bespoke script to fix a live dB.
Contributor

### freeeve commented Feb 10, 2014

 Ultra hack: http://console.neo4j.org/r/uoh8ll ``````reduce(acc=0, i IN idx | acc *10 + CASE substring(test,i,1) WHEN '1' THEN 1 WHEN '2' THEN 2 WHEN '3' THEN 3 WHEN '4' THEN 4 WHEN '5' THEN 5 WHEN '6' THEN 6 WHEN '7' THEN 7 WHEN '8' THEN 8 WHEN '9' THEN 9 ELSE 0 END ) AS converted ``````

### MatAtBread commented Feb 10, 2014

 Dirty, but sweet. Thx :)

### freethejazz commented Apr 2, 2014

 @jexp I'd think `./` would be better, particularly in the case where the divisor is between 0 and 1 and the user has left off the leading zero (`3/.4`). However, since there is a `floor` function, does it make sense to go the python route and make `/` 'regular' division and require the user to explicitly specify when they want to floor? I think this is more intuitive, but would also mean breaking changes(and thus part of the next major release and not any time sooner.)
Contributor

### cleishm commented Apr 2, 2014

 In the next release of Neo4j, Cypher will contain a `toFloat` method, that will allow for `toFloat(3)/4` to do as you're expecting. `toFloat` will also work with strings (and there will be a corresponding `toInt`).

### ccorcos commented Aug 18, 2015

 wait... no workaround this? So I cant compute a ratio of two integers?
Contributor

### cleishm commented Aug 18, 2015

 Cypher now supports `toFloat(...)`, which will allow you to do what you're asking for, e.g. `toFloat(3)/4`

### ccorcos commented Aug 18, 2015

 ok. sweet