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

**URL:** <https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935>\
**Category:** Elasticsearch\
**Created:** [January 31, 2015, 3:06am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935 "2015-01-31T03:06:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Chuen\_Ooi](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Lee\_Chuen\_Ooi](https://discuss.elastic.co/u/Lee_Chuen_Ooi)\
**Post date:** [January 31, 2015, 3:06am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/1 "2015-01-31T03:06:33Z")

</div>

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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 31, 2015, 7:00am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/2 "2015-01-31T07:00:02Z")

</div>

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](mailto: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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/F00311EF-F8B1-42A3-A853-1CDE6DF336F8%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Lee\_Chuen\_Ooi](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Lee\_Chuen\_Ooi](https://discuss.elastic.co/u/Lee_Chuen_Ooi)\
**Post date:** [January 31, 2015, 8:19am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/3 "2015-01-31T08:19:18Z")

</div>

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](mailto: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](mailto: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)  
> [https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com?utm\_medium=email&utm\_source=footer](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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 31, 2015, 8:27am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/4 "2015-01-31T08:27:04Z")

</div>

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](mailto:leechuen@gmail.com) 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](mailto: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](mailto: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).  
> > > For more options, visit [https://groups.google.com/d/optout](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/A0F75464-A9AD-4DDA-B8A1-97234B37ABBC%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Lee\_Chuen\_Ooi](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Lee\_Chuen\_Ooi](https://discuss.elastic.co/u/Lee_Chuen_Ooi)\
**Post date:** [February 2, 2015, 3:03am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/5 "2015-02-02T03:03:51Z")

</div>

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](mailto: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](mailto: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](mailto: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)  
> > [https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com?utm\_medium=email&utm\_source=footer](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](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](mailto: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)  
> > [https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com?utm\_medium=email&utm\_source=footer](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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/a2e01af1-9e5b-48a6-a5fc-ae8feb4df09a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 2, 2015, 6:14am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/6 "2015-02-02T06:14:12Z")

</div>

I guess something like this:

Math.round(7.8151\*1000)/1000.0

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

> Le 2 févr. 2015 à 04:03, Lee Chuen Ooi [leechuen@gmail.com](mailto: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.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](mailto:leec...@gmail.com) 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](mailto: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](mailto: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).  
> > > > > For more options, visit [https://groups.google.com/d/optout](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](mailto: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).  
> > > For more options, visit [https://groups.google.com/d/optout](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/a2e01af1-9e5b-48a6-a5fc-ae8feb4df09a%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/367C07DC-37BE-4B62-B267-4BDDC1ACF9FB%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Lee\_Chuen\_Ooi](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Lee\_Chuen\_Ooi](https://discuss.elastic.co/u/Lee_Chuen_Ooi)\
**Post date:** [February 13, 2015, 3:26pm UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/7 "2015-02-13T15:26:14Z")

</div>

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 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 2 févr. 2015 à 04:03, Lee Chuen Ooi \<[leec...@gmail.com](mailto: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.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](mailto:leec...@gmail.com) 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](mailto: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](mailto: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)  
> > > [https://groups.google.com/d/msgid/elasticsearch/575e72be-d273-4f85-988f-6e280f9937f6%40googlegroups.com?utm\_medium=email&utm\_source=footer](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](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](mailto: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)  
> > > [https://groups.google.com/d/msgid/elasticsearch/a1a89ae8-0bac-461a-844b-7e015790bb04%40googlegroups.com?utm\_medium=email&utm\_source=footer](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](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](mailto: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)  
> > [https://groups.google.com/d/msgid/elasticsearch/a2e01af1-9e5b-48a6-a5fc-ae8feb4df09a%40googlegroups.com?utm\_medium=email&utm\_source=footer](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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/04b4dcea-43ea-4337-afcc-71755b150e3f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:32am UTC](https://discuss.elastic.co/t/script-how-to-round-up-float-to-2-decimal-point/21935/8 "2017-07-06T00:32:55Z")

</div>


