【问题标题】:jsrender Format Decimal to show 3 places in tablejsrender Format Decimal 在表格中显示 3 个位置
【发布时间】:2020-12-04 09:59:36
【问题描述】:

我有 JSON 数据,它返回所有小数点后 9 位的数字。将数据格式化为每行仅显示 3 个小数的正确方法是什么。

<table>
  <td>LotNum</td>
  <td>Heat</td>
  <td>C</td>
  <td>Mn</td>
  <td>P</td>
  <td>S</td>
  <td>Si</td>
  <td>Al</td>
  <td>RB</td>
  {{for value}}
  <tr>
<td>{{:UD01_Key1}}</td>
<td>{{:PartLot_Heat_c}}</td>
<td>{{:UD03_Number01}}</td>
<td>{{:UD03_Number02}}</td>
<td>{{:UD03_Number03}}</td>
<td>{{:UD03_Number04}}</td>
<td>{{:UD03_Number05}}</td>
<td>{{:UD03_Number06}}</td>
<td>{{:UD03_Number07}}</td>

  </tr>

  {{/for}}
</table>

【问题讨论】:

    标签: jsrender


    【解决方案1】:

    JsRender 接受表达式,(参见​​https://www.jsviews.com/#paths),因此如果您的 UD03_Number01 是 number 类型,您可以这样做,例如:

    {{:UD03_Number01.toFixed(3)}}

    在此处查看类似问题:How to format decimal using JsRender templates,包括一些替代方法。

    如果 UD03_Number01 是一个字符串,你可以使用强制它来编号

    {{:(+UD03_Number01).toFixed(3)}}

    -- 否则您可以使用字符串操作方法,例如substr()。在这种情况下,您可能希望将您的字符串操作放在 converter 中。

    【讨论】:

    • 我似乎得到一个错误,它不是一个函数。执行模板引擎时出错。 data.UD03_Number01.toFixed is not a function TypeError: data.UD03_Number01.toFixed is not a function
    • 啊 - 你说它们是数字,但听起来它们是字符串类型。我更新了我的回复。
    • 优秀。就是这样,它都在 JSON 中,所以它是字符串。谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2012-11-06
    • 2019-09-21
    • 2011-10-07
    相关资源
    最近更新 更多