Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why django uses a comma as decimal separator

I am using python 2.6 and django 1.27

my model

class Plan(models.Model):
     price = models.DecimalField(max_digits=5, decimal_places=2,default=0)
     .....

in my template i have

{{plan.price}}

My problem is that on my local machine i get dot used as separator for example '2.54'
While on my production machine i get '2,54' - comma is used as separator.
I would like it to use dot everywhere.

in the django docs https://docs.djangoproject.com/en/1.2/ref/settings/#decimal-separator
it say there is the "DECIMAL_SEPARATOR" option the default it dot.

btw in both machines

In [2]: from django.conf import settings
In [3]: print settings.DECIMAL_SEPARATOR
.

SOLUTION:

as @Marcin pointed out
setting USE_L10N to False on production machine.

like image 594
yossi Avatar asked Oct 24 '13 13:10

yossi


People also ask

Why is a comma used instead of a decimal point?

as a separator between the dollars and cents. Some countries use a comma (,) instead of a decimal to indicate that separation. In addition, while the U.S. and a number of other countries use a comma to separate thousands, some countries use a decimal point for this purpose.

What does a comma in a decimal mean?

In the United States, we use the decimal or period (“.”) to represent the difference between whole numbers and partial numbers. We use the comma (“,”) to separate groups of three places on the whole numbers side. This might be different from the way you currently write numbers.

Why do Germans use commas instead of decimals?

In German the comma rather than the period is used as a decimal separator and the dot is used as a thousands separator: 12.345,67 . The comma creates some unwanted space when used as in $12.345,67$ .


2 Answers

You can use this alternative way directly on your template:

{% load l10n %}

{% localize off %}
{{ my_floatvar }}
{% endlocalize %}

or this one:

{% load l10n %}

{{ my_floatvar|unlocalize }}

More info in https://docs.djangoproject.com/en/dev/topics/i18n/formatting/#controlling-localization-in-templates

like image 191
Mario J. Barchéin Avatar answered Oct 28 '22 01:10

Mario J. Barchéin


First of all, I assume you have L10N and I18N turned on in your settings.py, because that's the default. The difference you see is likely because you are accessing the website from two different computers with two different locales. Django tries to format things for the locale reported by the browser.

However, you can disable this behaviour. See https://docs.djangoproject.com/en/dev/ref/settings/. Set USE_L10N=False, and set the various separator options specified on the linked page.

like image 22
Marcin Avatar answered Oct 28 '22 01:10

Marcin