Skip to content

Commit

Permalink
Applied number formatting suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
migurski committed Mar 15, 2017
1 parent f099476 commit 7d75f14
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
34 changes: 17 additions & 17 deletions code/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
#coverage tr th,
#coverage tr td.group,
#coverage tr td.area-detail,
#coverage tr td.population-detail,
#coverage tr td.density-stddev
#coverage tr td.population-detail
{
font-size: 65%;
}

#coverage tr th.numeric,
#coverage tr td.numeric
{
text-align: right;
}

#coverage tr th, #coverage tr td
{
text-align: left;
Expand Down Expand Up @@ -76,9 +81,9 @@
<th colspan="2">Country</th>
<th>ISO</th>
<th>Coverage Group</th>
<th colspan="2">Land Area Covered</th>
<th colspan="2">Population Covered</th>
<th colspan="2">Address Density per 1,000 People</th>
<th colspan="2" class="numeric">Land Area Covered</th>
<th colspan="2" class="numeric">Population Covered</th>
<th class="numeric">Addresses per 1,000 People</th>
</tr>
{% for area in areas %}
<tr>
Expand All @@ -94,26 +99,21 @@
Minimal
{% endif %}
</td>
<td>
<td class="numeric">
{{ area.area_pct|nice_percentage }}
</td>
<td class="area-detail">
<td class="numeric area-detail">
{{ area.area_total|nice_big_number }} km²
</td>
<td>
<td class="numeric">
{{ area.pop_pct|nice_percentage }}
</td>
<td class="population-detail">
<td class="numeric population-detail">
{{ area.pop_total|nice_big_number }} people
</td>
<td>
{% if area.cpp_avg %}
{{ (1000 * area.cpp_avg)|nice_integer }}
{% endif %}
</td>
<td class="density-stddev">
{% if area.cpp_avg %}
±{{ (1000 * area.cpp_stddev)|nice_integer }}
<td class="numeric">
{% if area.pop_total > 0 %}
{{ (1000 * area.addr_count / area.pop_total)|nice_integer }}
{% endif %}
</td>
</tr>
Expand Down
9 changes: 6 additions & 3 deletions code/web-app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
def get_index():
with psycopg2.connect(os.environ['DATABASE_URL']) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as db:
db.execute('''SELECT iso_a2, name, addr_count, area_total, area_pct,
pop_total, pop_pct, cpp_avg, cpp_stddev
db.execute('''SELECT iso_a2, name, addr_count, area_total,
area_pct, pop_total, pop_pct
FROM areas WHERE name IS NOT NULL ORDER BY name''')
areas = db.fetchall()

Expand Down Expand Up @@ -38,7 +38,7 @@ def filter_nice_flag(iso_a2):
def filter_nice_percentage(number):
''' Format a floating point number like '11%'
'''
if number >= 0.75:
if number >= 0.99:
return '{:.0f}%'.format(number * 100)

return '{:.1f}%'.format((number or 0) * 100)
Expand All @@ -47,6 +47,9 @@ def filter_nice_percentage(number):
def filter_nice_big_number(number):
''' Format a number like '99M', '9.9M', '99K', '9.9K', or '999'
'''
if number > 1000000:
return '{}K'.format(filter_nice_integer(number / 1000))

if number > 10000000:
return '{:.0f}M'.format(number / 1000000)

Expand Down

0 comments on commit 7d75f14

Please sign in to comment.