# Painless - how to convert float number to 'pt,BR' locale with one decimal digit?

**URL:** <https://discuss.elastic.co/t/painless-how-to-convert-float-number-to-pt-br-locale-with-one-decimal-digit/171221>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 7, 2019, 3:49am UTC](https://discuss.elastic.co/t/painless-how-to-convert-float-number-to-pt-br-locale-with-one-decimal-digit/171221 "2019-03-07T03:49:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)\
**Post date:** [March 7, 2019, 3:49am UTC](https://discuss.elastic.co/t/painless-how-to-convert-float-number-to-pt-br-locale-with-one-decimal-digit/171221/1 "2019-03-07T03:49:31Z")

</div>

Hi everyone,

I have a `script_field` for converting a float field value into an one digit value in 'pt,BR' locale. I'm doing the following:

```
script: {
            inline: "if (params['_source']['my_field'] != null) { [(Math.round(params['_source']['my_field'] * 100.0)/100.0) ] }"
        }

```

At this point, I've just rounded it to one digit decimal. I'd like to set a Locale format. Is it possible?

I've tried

```
script: {
            inline: "if (params['_source']['my_field'] != null) { [new Float(Float.toString(Math.round(params['_source']['my_field'] * 100.0)/100.0).replace('.', ',')) ] }"
        }

```

But got an exception, which I couldn't detect the real problem. Can't I use the `Float` class here?

Does anybody have a hint?

Thank you,

Guilherme

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [March 11, 2019, 5:37pm UTC](https://discuss.elastic.co/t/painless-how-to-convert-float-number-to-pt-br-locale-with-one-decimal-digit/171221/2 "2019-03-11T17:37:09Z")

</div>

In the second script try the following:

```auto
script: {
            inline: "if (params['_source']['my_field'] != null) { return Float.toString(Math.round((float)params['_source']['my_field'] * 100.0F)/100.0F).replace('.', ',')) }"
        }

```

The errors you are seeing are probably one that says Painless doesn't support new Float, instead use Float.valueOf. API ([https://www.elastic.co/guide/en/elasticsearch/painless/7.x/painless-api-reference.html](https://www.elastic.co/guide/en/elasticsearch/painless/7.x/painless-api-reference.html)). Then you will probably receive errors about how double cannot be cast to float. If you want to use float you will have to specify the values as floats using casting, otherwise values will be promoted to double in the script shown here ([https://www.elastic.co/guide/en/elasticsearch/painless/7.x/painless-casting.html#promotion](https://www.elastic.co/guide/en/elasticsearch/painless/7.x/painless-casting.html#promotion))

---

<div class="post-metadata">

**Author:** ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)\
**Post date:** [March 13, 2019, 1:24am UTC](https://discuss.elastic.co/t/painless-how-to-convert-float-number-to-pt-br-locale-with-one-decimal-digit/171221/3 "2019-03-13T01:24:26Z")

</div>

Thank you, @Jack_Conradson! It worked perfectly.

Regards!

---

<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:** [April 10, 2019, 1:24am UTC](https://discuss.elastic.co/t/painless-how-to-convert-float-number-to-pt-br-locale-with-one-decimal-digit/171221/4 "2019-04-10T01:24:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
