-
Notifications
You must be signed in to change notification settings - Fork 464
Allow full control on Decimal behaviour #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,58 @@ the specification. The following table is just a relatively brief overview. | |
| +----------+-----------------------------------------------------------------+ | ||
|
|
||
|
|
||
| Rounding Modes | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +++++++ Thanks for the great docs! Could you add a doctest or a note to the docstring for |
||
| ============== | ||
|
|
||
| Since Babel makes full use of Python's `Decimal`_ type to perform number | ||
| rounding before formatting, users have the chance to control the rounding mode | ||
| and other configurable parameters through the active `Context`_ instance. | ||
|
|
||
| By default, Python rounding mode is ``ROUND_HALF_EVEN`` which complies with | ||
| `UTS #35 section 3.3`_. Yet, the caller has the opportunity to tweak the | ||
| current context before formatting a number or currency: | ||
|
|
||
| .. code-block:: pycon | ||
|
|
||
| >>> from babel.numbers import decimal, format_decimal | ||
| >>> with decimal.localcontext(decimal.Context(rounding=decimal.ROUND_DOWN)): | ||
| >>> txt = format_decimal(123.99, format='#', locale='en_US') | ||
| >>> txt | ||
| u'123' | ||
|
|
||
| It is also possible to use ``decimal.setcontext`` or directly modifying the | ||
| instance returned by ``decimal.getcontext``. However, using a context manager | ||
| is always more convenient due to the automatic restoration and the ability to | ||
| nest them. | ||
|
|
||
| Whatever mechanism is chosen, always make use of the ``decimal`` module imported | ||
| from ``babel.numbers``. For efficiency reasons, Babel uses the fastest decimal | ||
| implementation available, such as `cdecimal`_. These various implementation | ||
| offer an identical API, but their types and instances do **not** interoperate | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... is this considered a non-backwards compatible change?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohhh nevermind, we were already using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, in terms of Babel API is fully backwards compatible. If your code manipulates the current decimal context while also using Babel, you may see slightly different results. But if you are playing with decimal precision and exponents, aside from the rounding mode, you should have seen this for some time already |
||
| with each other. | ||
|
|
||
| For example, the previous example can be slightly modified to generate | ||
| unexpected results on Python 2.7, with the `cdecimal`_ module installed: | ||
|
|
||
| .. code-block:: pycon | ||
|
|
||
| >>> from decimal import localcontext, Context, ROUND_DOWN | ||
| >>> from babel.numbers import format_decimal | ||
| >>> with localcontext(Context(rounding=ROUND_DOWN)): | ||
| >>> txt = format_decimal(123.99, format='#', locale='en_US') | ||
| >>> txt | ||
| u'124' | ||
|
|
||
| Changing other parameters such as the precision may also alter the results of | ||
| the number formatting functions. Remember to test your code to make sure it | ||
| behaves as desired. | ||
|
|
||
| .. _Decimal: https://docs.python.org/3/library/decimal.html#decimal-objects | ||
| .. _Context: https://docs.python.org/3/library/decimal.html#context-objects | ||
| .. _`UTS #35 section 3.3`: http://www.unicode.org/reports/tr35/tr35-numbers.html#Formatting | ||
| .. _cdecimal: https://pypi.python.org/pypi/cdecimal | ||
|
|
||
|
|
||
| Parsing Numbers | ||
| =============== | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains unused source code.
PyUnusedCodeBear, severity NORMAL, section
default.The issue can be fixed by applying the following patch:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains unused source code.
PyUnusedCodeBear, severity NORMAL, section
default.The issue can be fixed by applying the following patch:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems odd, according to Codecov,
babel/_compat.pyis 100% covered.