简体   繁体   中英

How to limit JavaScript API Request Decimal places

I am using the below code to fetch some data from an api. The response will always be a number however I want to limit the number of decimal places to 2 when I output this.

  $(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)) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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