簡體   English   中英

如何限制 JavaScript API 請求小數位數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM