1👍
✅
Probably the cleanest way will be to format this with the locale
. First check if the German locale
is supported on your system, for example with Debian:
$ locale -a
this will list all locale’s, the name can very over different system. If the locale does not appear, you can generate one with:
$ sudo locale-gen de_DE
$ sudo locale-gen de_DE.utf8
$ sudo update-locale
Now we can work with this locale to format the number:
import locale
class MyModel(models.Model):
amount = models.DecimalField(max_digits=12, decimal_places=2)
@property
def as_money(self):
locale.setlocale(local.LC_ALL, 'de_DE.utf8')
return locale.format('€ %.2f', self.amount, grouping=True, monetary=True)
You can then use this with a MyModel
object my_model
with my_model.as_money
.
Normally the currency sign is put in front of the amount, to prevent people from writing digits in front (whereas writing digits at the end is often less of a problem).
Source:stackexchange.com