简体   繁体   English

如何限制 JavaScript API 请求小数位数

[英]How to limit JavaScript API Request Decimal places

I am using the below code to fetch some data from an api.我正在使用以下代码从 api 中获取一些数据。 The response will always be a number however I want to limit the number of decimal places to 2 when I output this.响应将始终是一个数字,但是我想在输出时将小数位数限制为 2。

  $(document).ready(function sendRequest() {
        $.ajax({
          url: 'https://apiurl',
          type: 'GET',
          success: function(response) {
            $("#test").html(response);
          },
          error: function() {
            $('#errors').text("Error");
          }
        });
    });

I tried the below but this doesn't work.我尝试了以下,但这不起作用。

$("#test").html(response.toFixed(2) 

Any ideas?有任何想法吗?

Your response variable is string you need to convert your variable first您的响应变量是字符串,您需要先转换变量

var res = parseFloat(response) ;

$("#test").html(res.toFixed(2)) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM