Reputation: 145
I have tried looking around finding out how to format a number in python to this specific format. Formatting a number to a currency format was pretty easy to figure out but doing this was not.
<td>{{ "{:,.2f} NOK".format(acc['AccountBalance']) }}</td>
So I simply want to format 12345678901 to 1234.56.78901. I want to use this in my flask template website. Every number is always 11 digits, and it should be formatted as 4 digits period 2 digits period 5 digits period.
EDIT: This is being used in a Flask template, on numbers retrieved from an SQLite table and put into a list. It is the accs['ID'] that is getting this formatting done on it.
accs = db.execute(
'SELECT a.ID, ownerID, AccountBalance, AccountName'
' FROM atbl_bank_accounts a JOIN atbl_system_users u ON a.ownerID = u.ID'
' WHERE a.ownerID = ?',
(g.user['ID'],)
).fetchall()
This again is posted in to a table on the website using flask template as so:
<table class="table table-striped table-hover table-borderless mt-5">
<thead>
<tr>
<th class="col-2" scope="col">Account Number</th>
<th class="col-4" scope="col">Account Name</th>
<th class="col-4" scope="col">Amount left</th>
<th class="col-2" scope="col">Actions</th>
</tr>
</thead>
<tbody>
{% for acc in accs %}
<tr>
<th scope="row">{{ acc['ID'] }}</th>
<td>{{ acc['AccountName'] }}</td>
<!--<td>{{ acc['AccountBalance'] }}</td>-->
<td>{{ "{:,.2f} NOK".format(acc['AccountBalance']) }}</td>
<td>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Edit name of account"><i class="fas fa-edit"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Transfer money from account"><i class="fas fa-file-invoice-dollar"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Delete account"><i class="fas fa-trash"></i></button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
So I either need a solution that works straight in the HTML or in the python script. Apologies for not being clear enough.
I did search a fair bit around, but if I skipped an article about this I'm sorry. Thanks for the answers in advance.
Upvotes: 0
Views: 894
Reputation: 145
Thanks to NoNickAvailable and wwii for the tips. Since I was using this is with a flask template and for loop, I couldn't use wwii's answer directly. With help from this stackoverflow article, I managed to make a working version.
I retrive the data from the database as follows:
accs = db.execute(
'SELECT a.ID, ownerID, AccountBalance, AccountName'
' FROM atbl_bank_accounts a JOIN atbl_system_users u ON a.ownerID = u.ID'
' WHERE a.ownerID = ?',
(g.user['ID'],)
).fetchall()
Then I had to use dict
:
IDrows = [dict(row) for row in accs]
Then to format the numbers with my very specific format I used what wwii and NoNickAvailable answered:
for i in IDrows:
i['ID'] = '.'.join((str(i['ID'])[:4],str(i['ID'])[4:-5],str(i['ID'])[-5:]))
The numbers were than all individually formatted and easily inserted into my table:
<table class="table table-striped table-hover table-borderless mt-5">
<thead>
<tr>
<th class="col-2" scope="col">Account Number</th>
<th class="col-4" scope="col">Account Name</th>
<th class="col-4" scope="col">Amount left</th>
<th class="col-2" scope="col">Actions</th>
</tr>
</thead>
<tbody>
{% for acc in IDrows %}
<tr>
<th scope="row">{{ acc['ID'] }}</th>
<td>{{ acc['AccountName'] }}</td>
<!--<td>{{ acc['AccountBalance'] }}</td>-->
<td>{{ "{:,.2f} NOK".format(acc['AccountBalance']) }}</td>
<td>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Edit name of account"><i class="fas fa-edit"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Transfer money from account"><i class="fas fa-file-invoice-dollar"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Delete account"><i class="fas fa-trash"></i></button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
Upvotes: 0
Reputation: 23753
Turn the integer to a str and use slices along with join
.
In [22]: p = 12345678901
In [23]: q = str(p)
In [24]: '.'.join((q[:4],q[4:-5],q[-5:]))
Out[24]: '1234.56.78901'
Or you could use slice objects which is basically the same thing but you get to name them.
In [33]: first, middle, last = slice(0,4),slice(4,-5),slice(-5,None)
In [34]: '.'.join((q[first],q[middle],q[last]))
Out[34]: '1234.56.78901'
Upvotes: 2