javascript - 如何限制 JavaScript API 请求小数位数
问题描述
我正在使用下面的代码从 api 获取一些数据。响应将始终是一个数字,但是我想在输出时将小数位数限制为 2。
$(document).ready(function sendRequest() {
$.ajax({
url: 'https://apiurl',
type: 'GET',
success: function(response) {
$("#test").html(response);
},
error: function() {
$('#errors').text("Error");
}
});
});
我尝试了以下方法,但这不起作用。
$("#test").html(response.toFixed(2)
有任何想法吗?
解决方案
您的响应变量是字符串,您需要先转换变量
var res = parseFloat(response) ;
$("#test").html(res.toFixed(2)) ;
推荐阅读
- excel - Excel 到 Word 的查找和替换 - 如何绕过 255 个字符的限制
- excel - 求月末加班总结的Excel公式
- java - auto.register.schemas 是否应该在修改 .avsc 时在模式注册表中创建一个新版本?
- android - Android Studio 错误地保存密码--添加了额外的字符
- python - 如何从地图中一次检索 2 个值 - 在 Python 3.x 中
- java - 如何使用 android studio 中的 MediaStore Api 在 android 11 中获取特定文件夹,如 WhatsApp (.Status) 文件夹
- elasticsearch - 过滤器不适用于弹性搜索中嵌套字段的非嵌套聚合
- python - Neo4J python驱动程序在运行到docker容器时不会引发错误
- woocommerce - 获取产品变体描述以显示在 woocommerce 中的订单项目上?
- tomcat - Jersey jax-rs 应用程序在使用 Tomcat 9 的端点上返回 404 错误