10

Everytime I try to divide something in neo4j, I keep getting zero. I am using the following query:

MATCH (m:Member)-[:ACTIVITY{issue_d:"16-Jan"}]->(l:Loan)
MATCH (m)-[:ACTIVITY]->(p:Payments)
WHERE l.installment<1000 AND p.total_pymnt>0
RETURN (l.funded_amnt-p.total_pymnt),(l.funded_amnt-p.total_pymnt)/(l.funded_amnt), l.funded_amnt, p.total_pymnt, m.member_id
LIMIT 1000;

I checked to make sure that my values for funded_amnt and total_pymnt are not messing up the operation they seem good:

screenshot - query result

Even when I just do:

500/l.funded_amnt 

I still get zero. What am I doing wrong?

Aly
  • 241
  • 2
  • 11

1 Answers1

17

Multiply your numerator by 1.0.

MATCH (m:Member)-[:ACTIVITY {issue_d:"16-Jan"}]->(l:Loan)
MATCH (m)-[:ACTIVITY]->(p:Payments)
WHERE l.installment < 1000 AND p.total_pymnt > 0
RETURN (l.funded_amnt - p.total_pymnt), ((l.funded_amnt - p.total_pymnt) * 1.0) / l.funded_amnt, l.funded_amnt, p.total_pymnt, m.member_id
LIMIT 1000;

Integer division discards the remainder, so you need to multiply your numerator by 1.0 or wrap it in toFloat().

RETURN 5 / 12;          // 0
RETURN 5 * 1.0 / 12;    // 0.4166666666666667
RETURN toFloat(5) / 12; // 0.4166666666666667
Nicole White
  • 7,720
  • 29
  • 31