javascript - 我想将较大的小数解析为从 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>
解决方案
将您的 Number 转换为字符串,然后从中提取前 5 个字符,然后将其转换为 Number :
Number(value.remainingstorage.toString().substring(0, 5));
推荐阅读
- kubernetes - Kubernetes 为每个 pod 重定向子域请求
- google-apps-script - Apps 脚本:在添加新行之前查找并删除重复项
- c# - C# SerialPort WriteLine 卡住了
- python - 为 Python 安装 LSSVR
- java - 我如何更改在 jhipster 中显示这些消息的顺序(最新消息在顶部)
- php - 如何将数据放入mysql数据库[错误]
- javascript - 窗口宽度小于 600px 时如何禁用 MUI Grid 项堆叠
- yaml - 在规则关键字之前覆盖 gitlab-ci 管道变量
- counter - PSoC 5,作为编码器的 Counter UpCnt 和 DwnCnt 模式
- kubernetes - 使用 Kuberentes 对象进行悲观锁定