0

I'm trying to sum revenue from orders, But the results are in float .6299743652344

Any idea why?

Field in data is .00

("grand_total": 84.5)

s = Search(using=ES, index='orders')
s = s[:0]
s.aggs.metric('rev', 'sum', field='grand_total')
res = s.execute()
return res.aggs.rev.value

result: Response: {'rev': {'value': 3195.6299743652344}}

JoySalomon
  • 61
  • 1
  • 10
  • This answer might help: https://stackoverflow.com/a/53480867/4604579 (hint: float number precision in JS) – Val Dec 23 '19 at 15:22
  • Didn't help, but thank you. – JoySalomon Dec 26 '19 at 16:15
  • How come? It is most likely a precision issue – Val Dec 26 '19 at 16:16
  • It's doesn't makes sense, the numbers are rounded. the sum should be rounded to. – JoySalomon Dec 30 '19 at 12:30
  • Let's take `84.1 * 84.6` which should give you `7114.86`, if you try to compute that in JavaScript (Node.js console, browser dev tools, etc), you'll get `7114.859999999999` – Val Dec 30 '19 at 12:33
  • Same thing with `81.1 + 82.8`, it should give you `163.9`, yet in JS it yields `163.89999999999998` – Val Dec 30 '19 at 12:35
  • I understand that this is the response in JS, still it's weird that ElasticSearch SUM results is not rounded. – JoySalomon Dec 30 '19 at 13:10
  • The reason is simple: The response is in JSON. This might also help: https://stackoverflow.com/questions/35709595/why-would-you-use-a-string-in-json-to-represent-a-decimal-number – Val Dec 30 '19 at 13:12

0 Answers0