Script - how to round up Float to 2 decimal point?

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 elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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 leechuen@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:
calculate (field1 + field2) * field3 -> result 1
round up the result of step (1) to 2 decimal points -> result 2
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 elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/F00311EF-F8B1-42A3-A853-1CDE6DF336F8%40pilato.fr.
For more options, visit https://groups.google.com/d/optout.

Hi,
It doesn't work. I tried before posting this thread. :frowning:

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 <javascript:>>
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 <javascript:>.
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 elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

And this?

Math.round(doc['totals.tax'].value * 100.0)/100.0

David

Le 31 janv. 2015 à 09:19, Lee Chuen Ooi leechuen@gmail.com a écrit :

Hi,
It doesn't work. I tried before posting this thread. :frowning:

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:
calculate (field1 + field2) * field3 -> result 1
round up the result of step (1) to 2 decimal points -> result 2
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.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/A0F75464-A9AD-4DDA-B8A1-97234B37ABBC%40pilato.fr.
For more options, visit https://groups.google.com/d/optout.

1 Like

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.8151100)/100 -> It returns 7.
round(7.8151) -> It returns 8.
Math.round(7.8151
100)/100.0 -> It returns 7.82
Math.round(7.8151100.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. :frowning:

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+unsubscribe@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.

I guess something like this:

Math.round(7.8151*1000)/1000.0

--
David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 2 févr. 2015 à 04:03, Lee Chuen Ooi leechuen@gmail.com a écrit :

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.8151100)/100 -> It returns 7.
round(7.8151) -> It returns 8.
Math.round(7.8151
100)/100.0 -> It returns 7.82
Math.round(7.8151100.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 a écrit :

Hi,
It doesn't work. I tried before posting this thread. :frowning:

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:
calculate (field1 + field2) * field3 -> result 1
round up the result of step (1) to 2 decimal points -> result 2
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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com.
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/367C07DC-37BE-4B62-B267-4BDDC1ACF9FB%40pilato.fr.
For more options, visit https://groups.google.com/d/optout.

1 Like

Hi David,
Yeah, it works.

Thanks.

On Monday, February 2, 2015 at 2:14:24 PM UTC+8, David Pilato wrote:

I guess something like this:

Math.round(7.8151*1000)/1000.0

--
David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 2 févr. 2015 à 04:03, Lee Chuen Ooi <leec...@gmail.com <javascript:>>
a écrit :

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.8151100)/100 -> It returns 7.
round(7.8151) -> It returns 8.
Math.round(7.8151
100)/100.0 -> It returns 7.82
Math.round(7.8151100.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 a écrit :

Hi,
It doesn't work. I tried before posting this thread. :frowning:

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.
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 elasticsearc...@googlegroups.com <javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/a2e01af1-9e5b-48a6-a5fc-ae8feb4df09a%40googlegroups.com
https://groups.google.com/d/msgid/elasticsearch/a2e01af1-9e5b-48a6-a5fc-ae8feb4df09a%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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/04b4dcea-43ea-4337-afcc-71755b150e3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.