djangosnippets.org: Latest snippets tagged with 'integer'https://djangosnippets.org/tags/integer/2013-08-23T04:40:29.542756-05:00Create a random integer in a template
2013-08-23T04:40:29.542756-05:00bartTChttps://djangosnippets.org/snippets/2984/<p>Create a random integer with given length.</p>
<ul>
<li>For a length of 3 it will be between 100 and 999.</li>
<li>For a length of 4 it will be between 1000 and 9999.</li>
</ul>
<p>Use it in a template like:</p>
<pre><code>{% random_number as my_id %}
The id is {{ my_id }}
</code></pre>
Freely redistributableDjango enumeration for model field choices
2012-03-28T05:31:23.120525-05:00martinthenexthttps://djangosnippets.org/snippets/2725/<p>The problem with supplying a Django model field with choices parameter is the way you check a value of that field in an object. You do nasty things like this:</p>
<pre><code>if model_instance.choice_field == 1:
</code></pre>
<p>The problem of getting rid of hard-coded numbers is recognized over the internet, but I haven't …</p>
Freely redistributableConverts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits)
2012-03-14T16:02:38.243837-05:00pikhovkinhttps://djangosnippets.org/snippets/2716/<p>Converts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits)</p>
Freely redistributableStoring IP address ModelField as Integer, without nasty hacks..
2011-07-19T07:15:12.577979-05:00sleepycalhttps://djangosnippets.org/snippets/2493/<p>See code</p>
Freely redistributableInteger Currency Input
2008-09-04T23:24:10.323928-05:00jgeewaxhttps://djangosnippets.org/snippets/1023/<p>This accepts values such as $1,000,000 and stores them to the database as integers. It also re-renders them to the screen using the django.contrib.humanize.intcomma method which takes 1000000 and turns it into 1,000,000. Useful for large currency fields where the decimals aren't really necessary.</p>
Freely redistributableInteger list to pattern function
2007-12-01T07:51:21.735073-06:00marinhohttps://djangosnippets.org/snippets/496/<p>This function can be util for transform pattern lists like these to strings:</p>
<pre><code>>>> list_to_pattern([42, 43, 44, 45])
'42-45'
>>> list_to_pattern([15, 49, 50, 51, 52])
'15,49-52'
>>> list_to_pattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
'0-13'
</code></pre>
<p>You can use also the pattern to list …</p>
Freely redistributablePattern to integer list function
2007-12-01T07:49:03.788011-06:00marinhohttps://djangosnippets.org/snippets/495/<p>This function can be util for transform pattern strings like these to list:</p>
<pre><code>>>> pattern_to_list('42-45')
[42, 43, 44, 45]
>>> pattern_to_list('15,49-52')
[15, 49, 50, 51, 52]
>>> pattern_to_list('0-13')
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
</code></pre>
<p>You can use also the list to pattern …</p>
Freely redistributable