[英]How to limit JavaScript API Request Decimal places
我正在使用以下代码从 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)) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.