Reputation: 3
I have some values called from an API in a course I'm doing which I would like to format improve visibility. What is the best way to code this? Thanks. Example code is below:
${{list_item.marketCap}} {{list_item.ytdChange}}%
Where, the first one I would like to add a comma for thousands and 2dp, and the second times by 100?
Upvotes: 0
Views: 187
Reputation: 829
You can change the first one to 2dp with a comma like so...
{{ "{:,.2f}".format(float(list_item.marketCap)) }}
And to get the second item * 100 you simply return...
{{ float(list_item.ytdChange) * 100 }}
Hopefully that works for you!
Upvotes: 2
Reputation: 798
There are two options that I would suggest:
context
of the render
view return statement.For the format function, there is nothing really complicated here. You can just use string concatenation or the Python String Interpolation.
def format_thousands(value):
return f'{value:,}' # Using Python ≥3.6
def format_two_decimals(value):
return round(value, 2)
For more information on context
and views
, please refer to the Django Doc since it is the basic stuff not related to that question.
Upvotes: 0