Add filter for formatting dollar values
This commit is contained in:
parent
2f62aebd57
commit
cfc76f5137
@ -3,5 +3,14 @@ def iconSvg(name):
|
||||
return contents.read()
|
||||
|
||||
|
||||
def dollars(value):
|
||||
try:
|
||||
numberValue = float(value)
|
||||
except ValueError:
|
||||
numberValue = 0
|
||||
return "${:,.0f}".format(numberValue)
|
||||
|
||||
|
||||
def register_filters(app):
|
||||
app.jinja_env.filters['iconSvg'] = iconSvg
|
||||
app.jinja_env.filters['dollars'] = dollars
|
||||
|
@ -116,7 +116,7 @@
|
||||
<td>{{ r['full_name'] }}</td>
|
||||
<td></td>
|
||||
{% endif %}
|
||||
<td>${{ r['annual_usage'] }}</td>
|
||||
<td>{{ r['annual_usage'] | dollars }}</td>
|
||||
<td>{{ r['status'] }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
15
tests/test_filters.py
Normal file
15
tests/test_filters.py
Normal file
@ -0,0 +1,15 @@
|
||||
import pytest
|
||||
|
||||
from atst.filters import dollars
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input,expected", [
|
||||
('0', '$0'),
|
||||
('123.00', '$123'),
|
||||
('1234567', '$1,234,567'),
|
||||
('-1234', '$-1,234'),
|
||||
('one', '$0'),
|
||||
])
|
||||
def test_dollar_fomatter(input, expected):
|
||||
assert dollars(input) == expected
|
||||
|
Loading…
x
Reference in New Issue
Block a user