# Relatorio: How to locale-format a Python Decimal and preserve its precision?

**URL:** https://discuss.tryton.org/t/relatorio-how-to-locale-format-a-python-decimal-and-preserve-its-precision/3697
**Category:** Developer
**Tags:** relatorio
**Created:** [January 17, 2021, 5:55pm UTC](https://discuss.tryton.org/t/relatorio-how-to-locale-format-a-python-decimal-and-preserve-its-precision/3697 "2021-01-17T17:55:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)
#### Post date: [January 17, 2021, 5:55pm UTC](https://discuss.tryton.org/t/relatorio-how-to-locale-format-a-python-decimal-and-preserve-its-precision/3697/1 "2021-01-17T17:55:12Z")

</div>

When using relatorio outside of tryton, I want to locale-format Python `Decimal`s. How can this be done?

Notes:

1. [This stackoverflow answer](https://stackoverflow.com/questions/31910741/) shows a nice solution: `{0:n}'.format(d)` (after setting the locale, of course)
2. I tried to find how to use this in relatorio or genshi, but the [genshi documentation](https://genshi.edgewall.org/) does not actually explain how “variables” are resolved by the template engine. (At least I was not able to spot it).

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [January 19, 2021, 7:12am UTC](https://discuss.tryton.org/t/relatorio-how-to-locale-format-a-python-decimal-and-preserve-its-precision/3697/2 "2021-01-19T07:12:31Z")

</div>

Tryton reports have some [helper functions](https://hg.tryton.org/trytond/file/9da9d1618bec/trytond/report/report.py#l240) which you can use to format deciaml or floats using the locale of a language.

If you do not set any language it will use the language of the current user.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [January 19, 2021, 9:48am UTC](https://discuss.tryton.org/t/relatorio-how-to-locale-format-a-python-decimal-and-preserve-its-precision/3697/3 "2021-01-19T09:48:20Z")

</div>

The question was about Relatorio without Tryton.

> [@htgoebel](#):
>
> When using relatorio outside of tryton, I want to locale-format Python `Decimal`s. How can this be done?

You need to use the available tools of Python like [locale.format](https://docs.python.org/3.8/library/locale.html#locale.format) ( ⚠ setting locale is not threadsafe). Or you need to use third-party library like [Babel](http://babel.pocoo.org/en/latest/).

> [@htgoebel](#):
>
> how “variables” are resolved by the template engine

With the keyword argument passed to the report instance as context.

---

<div class="post-metadata">

### Author: ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)
#### Post date: [January 19, 2021, 10:10am UTC](https://discuss.tryton.org/t/relatorio-how-to-locale-format-a-python-decimal-and-preserve-its-precision/3697/4 "2021-01-19T10:10:20Z")

</div>

> [@ced](#):
>
> You need to use the available tools of Python like [locale.format](https://docs.python.org/3.8/library/locale.html#locale.format) ( ⚠ setting locale is not threadsafe). Or you need to use third-party library like [Babel](http://babel.pocoo.org/en/latest/).

Do I understand correctly: The values have to be formatted by the model, relatorio can’t do this.
