Hi David, It works with 100.0. It rounds up to 2 decimal points. But, what if we need to round it up to 3 or x decimal points?
Rounding up a FLOAT, DOUBLE datatype to x decimal points is a common business requirement. I wonder why there isn't any build-in function in ElasticSearh to round up to x decimal points (I can't find it from documentation). Here is the outcome of my testing: Math.round(7.8151*100)/100 -> It returns 7. round(7.8151) -> It returns 8. Math.round(7.8151*100)/100.0 -> It returns 7.82 Math.round(7.8151*100.0)/100.0 -> It returns 7.82 Math.round(7.8151*100.00)/100.00 -> It returns 7.82 Math.round(7.8151*100.000)/100.000 -> It returns 7.82 On Saturday, January 31, 2015 at 4:27:28 PM UTC+8, David Pilato wrote: > > And this? > > Math.round(doc['totals.tax'].value * 100.0)/100.0 > > David > > Le 31 janv. 2015 à 09:19, Lee Chuen Ooi <leec...@gmail.com <javascript:>> > a écrit : > > Hi, > It doesn't work. I tried before posting this thread. :-( > > Here is what I use: > > "script" : "Math.round(doc['totals.tax'].value * 100)/100" > > The result is an integer. > > > > > On Saturday, January 31, 2015 at 3:00:19 PM UTC+8, David Pilato wrote: >> >> Is this not working? Found it on stack over flow. >> >> Math.round(x * 100) / 100 >> >> >> >> David >> >> Le 31 janv. 2015 à 04:06, Lee Chuen Ooi <leec...@gmail.com> a écrit : >> >> Hi, >> >> Each doc has : >> >> - field1 float datatype. For e.g., 99.9999 >> - field2 float datatype. For e.g., 7.991 >> - field3 float datatype. For eg., 7.5007500800 >> - field4 float with 2 decimal points. For e.g. 8.10 >> >> >> I need to search doc by filtering out the result of the steps below: >> >> 1. calculate (field1 + field2) * field3 -> result 1 >> 2. round up the result of step (1) to 2 decimal points -> result 2 >> 3. If result of step (2) <> field4, return the doc in the search >> result >> >> >> I use "script" to achieve this. But, based on the documentation, there is >> limited groovy build in function available. There is no sum function. I >> need to use field1 * field3 + field2 * field3 to get result 1 as a >> workaround. >> >> I am stuck at Step 2. I am not able to round up the result of (field1 + >> field2) * field3 to 2 decimal point. How can it be done? Pls suggest. >> >> If you have a better way to get it done, other than using "script", pls >> suggest too. >> >> Thanks. >> >> >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "elasticsearch" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to elasticsearc...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com >> >> <https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> >> -- > You received this message because you are subscribed to the Google Groups > "elasticsearch" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to elasticsearc...@googlegroups.com <javascript:>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com > > <https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > -- You received this message because you are subscribed to the Google Groups "elasticsearch" group. To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a2e01af1-9e5b-48a6-a5fc-ae8feb4df09a%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.