首页 > 解决方案 > 我想将较大的小数解析为从 JSON 到 Html 表的小数

问题描述

'我在 Html 中创建了表格,表格显示小数,如我的 JSON 文件中所示(剩余存储和总存储)。我想表格只显示 5 个小数,而不像现在 12。我尝试解析但没有成功。有人可以帮我怎么做解决这个问题?

[{"acceptingcontracts":true,"netaddress":"22.169.100.43","remainingstorage":119722213376,"totalstorage":119722213376}]

'还有我的 html 代码'

$(document).ready(function() {
  $.getJSON("sample.json", function(data) {
    var employee_data = '';
    $.each(data, function(key, value) {
      employee_data += '<tr>';
      employee_data += '<td>' + value.netaddress + '</td>';
      employee_data += '<td>' + value.totalstorage + '</td>';
      employee_data += '<td>' + value.remainingstorage + '</td>';
      employee_data += '<td>' + value.acceptingcontracts + '</td>';
      employee_data += '</tr>';
    });
    $('#employee_table').append(employee_data);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
  <div class="table-responsive">
    <h1>View</h1>
    <br />

    <table class="table table-bordered table-striped" id="employee_table">
      <tr>
        <th>Name</th>
        <th>Total storage</th>
        <th>Remaining storage</th>
        <th>View</th>
      </tr>
    </table>
  </div>
</div>

标签: javascript

解决方案


将您的 Number 转换为字符串,然后从中提取前 5 个字符,然后将其转换为 Number :

Number(value.remainingstorage.toString().substring(0, 5));

推荐阅读