jsrender

jsrender Format Decimal to show 3 places in table


I have JSON data that returns all the numbers with 9 decimals. What is the correct way to format the data to only show 3 decimals in each row.

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

Solution

  • JsRender accepts expressions, (see https://www.jsviews.com/#paths), so if your UD03_Number01 is of type number, you can do, for example:

    {{:UD03_Number01.toFixed(3)}}

    See a similar question here: How to format decimal using JsRender templates, including some alternative approaches.

    If UD03_Number01 is a string, you can coerce it to number using

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

    -- or else you can use string manipulation methods, such as substr(). In that case you may want to put your string manipulation in a converter.