#24382: django.utils.numberformat should be UTF-8 safe
-------------------------------------+-------------------------------------
     Reporter:  jrief                |                    Owner:
         Type:                       |  alainivars
  Cleanup/optimization               |                   Status:  new
    Component:  Utilities            |                  Version:  1.8alpha1
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by jrief):

 Well, without making {{{numberformat.py}}} UTF-8 safe, I get this error:
 {{{
     str_number = '{:f}'.format(number)
 UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in
 position 0: ordinal not in range(128)
 }}}
 in
 
https://github.com/django/django/blob/stable/1.8.x/django/utils/numberformat.py#L28


 You can emulate this by implementing a special Decimal class, say
 {{{
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 from decimal import Decimal

 class EuroDecimal(Decimal):
     """
     Wrapper for Decimal which prefixes each amount with the € symbol.
     """
     def __format__(self, specifier, context=None, _localeconv=None):
         amount = Decimal.__format__(self, specifier, context, _localeconv)
         return '€ {}'.format(amount)

 # print amount in Euros using the current locale
 from django.utils.formats import number_format

 price = EuroDecimal('1.23')
 print number_format(price)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24382#comment:13>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.887ad3b1114fe3f2a706b9ddaa354489%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to