question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bypass number rounding

See original GitHub issue

babel.numbers.format_decimal() and babel.numbers.format_currency() have a built-in banker rounding implemented. See: https://github.com/python-babel/babel/blob/3ec7bb104e808d1fda16fbf9fec4b9d7efccaa3f/babel/numbers.py#L648-L649

If we need to localize numbers without rounding, we can’t use these methods.

The minimal default decimal precision per method are:

  • babel.numbers.format_decimal() => 3 digits after the dot
  • babel.numbers.format_currency() => 2 digits after the dot

Proof:

>>> import babel
>>> set([babel.Locale.parse(l).decimal_formats._data[None].frac_prec
...      for l in babel.localedata.locale_identifiers()])
set([(0, 6), (0, 3)])
>>>
>>> import babel
>>> set([babel.Locale.parse(l).currency_formats._data[None].frac_prec
...      for l in babel.localedata.locale_identifiers()])
set([(2, 2)])
>>>

So if you have monetary amounts to localize, and they’re already rounded to 2 trailing digits, then you’re lucky. Using any of the format_*() methods will have no side effects.

But for other numbers with higher precision, using format_*() as-is is dangerous. It will introduce unwanted rounding. For example, calling format_currency(0.9999, 'EUR', locale='fr') will return 1,00 €. I expect here to get the pristine 0,9999 € string.

I think there must be a clean and documented way to bypass arbitrary rounding when localizing numbers.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
etanolcommented, Apr 7, 2016

Allright fellows, I think I know how to solve this.

I’ve reached the conclusion that the most versatile solution to this problem is to add a new optional parameter to format_decimal and format_currency to be filled with a decimal.Context instance.

While remaining backwards compatible, this solution will allow full control on decimal number operations. That means that not only users will be able to change the rounding mode, but also control precision, exponent ranges and so on.

I’ll give it a try this weekend and will submit a pull request.

0reactions
kdeldyckecommented, Apr 24, 2017

This issue is addressed by #494.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Get Excel Not to Round Numbers - YouTube
In this video, I demonstrate how to stop Excel from rounding numbers. In my scenario I have a list of numbers all with...
Read more >
How to Stop Excel from Rounding Numbers (Decimals ...
Stop Rounding Number By Increasing Column Width​​ And in case the width of the column in which the number is entered is not...
Read more >
[Solved] How to skip rounding off - CodeProject
Solution 1. You can either cast[^] the double to an int or use the Math. Truncate[^] function.
Read more >
Truncate number to two decimal places without rounding
General solution to truncate (no rounding) a number to the n-th decimal digit and convert it to a string with exactly n decimal...
Read more >
How to bypass material rounding rules when using ...
As the material has rounding rule, thus, when I create the PO using this BAPI, the quantity will be rounded according to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found