简体   繁体   中英

HTML table display only two decimal points and format as a currency. (found Javascript function but don't know how to implement)

I am trying to have my html table only display 2 decimals points and have it format the last four columns of my tables as currency. I found this function online which is Javascript but I don't know how to implement it. Could someone help?

Here someone lays out the Javascript code: https://css-tricks.com/snippets/javascript/format-currency/

this is my code:

{% extends "layout.html" %}

{% block main %}
    <table style="width:100%" class="table">

        <tr>
            <th> ID </th>
            <th> Username </th>
            <th> Stock </th>
            <th> Amount </th>
            <th> Current Price </th>
            <th> Total Value </th>
            <th> Current Cash </th>
            <th> Grand Total </th>
        </tr>

            <tr>
                <td> {{ user_id }} </td>
                <td> {{ username }} </td>
                <td> {{ stocks }} </td>
                <td> {{ amount }} </td>
                <td> {{ price }} </td>
                <td> {{ (amount * price) }} </td>
                <td> {{ current_cash }} </td>
                <td> {{ grand_tot }} </td>

            </tr>


</table>

{% endblock %}

It is simple, just use the same function, CurrencyFormatted, that he has mentioned, and use the following code:

<tr>
    <td> {{ user_id }} </td>
    <td> {{ username }} </td>
    <td> {{ stocks }} </td>
    <td> {{ amount }} </td>
    <td> {{ CurrencyFormatted(price) }} </td>
    <td> {{ CurrencyFormatted(amount * price) }} </td>
    <td> {{ CurrencyFormatted(current_cash) }} </td>
    <td> {{ CurrencyFormatted(grand_tot) }} </td>
</tr>

You can use pipe in HTML to proper currency format:| currency:"INR" currency format:| currency:"INR" :

{{ Amount| currency:"INR"}}

Output:

 ₹100.00
<td>{{item.GradeAVG.toFixed(0)}}</td>
<td>{{item.TarazAVG.toFixed(2)}}</td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM