#25762: Optimize numberformat.format
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: tim-
Type: | mccurrach
Cleanup/optimization | Status: assigned
Component: Utilities | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by tim-mccurrach):
The approach used in the linked PR
(https://github.com/django/django/pull/5668/files) actually has a problem.
The following approach is used to avoid the rounding behaviour of the
builtin `format` function:
{{{
# If `number` is 10.888 and `decimal_pos` is 2 return
10.88 not 10.89.
format_string = '.%sf' % (decimal_pos + 1)
}}}
and the string is then truncated to `decimal_pos` decimals places.
However, for a number such as 10.999, this will still be rounded 11.00.
This is inconsistent with the behaviour of the rest of the function, which
truncates and doesn't round.
We therefore can't use the bultin `format` to do any kind of decimal
formatting as there doesn't appear (as it currently stands) to be an
option to truncate with it, however the speed advantages it offers can
still be used for the common case of seperating a number into thousands.
Using the test cases in `bench_number_format.py` linked above, an improved
function performs anywhere between 2x and 5x faster for most cases, and
again in the worst case is roughly the same (fluctuating between +2% and
-2% compared with the original function).
I'll add a new PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/25762#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.eda003801fef1f4c3138e5f516d27263%40djangoproject.com.