簡體   English   中英

HTML 表格僅顯示兩個小數點並以貨幣格式顯示。 (找到Javascript函數但不知道如何實現)

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

我試圖讓我的 html 表格只顯示 2 個小數點,並將表格的最后四列格式化為貨幣。 我在網上找到了這個函數,它是 Javascript,但我不知道如何實現它。 有人可以幫忙嗎?

這里有人列出了 Javascript 代碼: https ://css-tricks.com/snippets/javascript/format-currency/

這是我的代碼:

{% 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 %}

很簡單,只需使用他提到的同一個函數 CurrencyFormatted,並使用以下代碼:

<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>

您可以在HTML中使用pipe來轉換正確的currency format:| currency:"INR" currency format:| currency:"INR"

{{ Amount| currency:"INR"}}

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM