0

I'm having problems with my (javascript) API. When I use the coinmarketcap API (https://api.coinmarketcap.com/v1/ticker). As for "max_supply" for bitcoin, it gives me "16865112.0" in text. This is a problem. I want to automatically put comma's in the number like 16,865,112.0.

If the data id is for example ethereum (it doesnt have a max supply) it gives me ∞. That works.

Original:

$.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {
  for (var i = 0; i < data.length - 1; i++) {
    if (data[i].id == "bitcoin") {
      $("#max_supply").html(data[i].max_supply == null ? '∞' : data[i].max_supply);
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="max_supply"></div>

This gives me an output of "21000000.0"

This is what i got so far

$.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {
  for (var i = 0; i < data.length - 1; i++) {
    if (data[i].id == "bitcoin") {
      $("#max_supply").html(Number(data[i].max_supply).toLocaleString('en-US') == null ? '∞' : data[i].max_supply.toLocaleString('en-US'));
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="max_supply"></div>

This doesn't give me an output.

Any suggestions?

9
  • No way has this not already been answered on this site. Commented Feb 15, 2018 at 18:12
  • 1
    Possible duplicate of How to print a number with commas as thousands separators in JavaScript Commented Feb 15, 2018 at 18:15
  • @Feathercrown I have looked at other asked question but I couldnt figure it out, so that why i'm asking Commented Feb 15, 2018 at 18:17
  • @PatrickHund I have looked at that question but I still couldnt figure out what i have done wrong Commented Feb 15, 2018 at 18:18
  • Can you supply the code where you tried it? Commented Feb 15, 2018 at 18:19

1 Answer 1

1

First off you should take off the -1 in your for loop, or you will be missing the last item.

The ternary maxSupply = maxSupply == null ? '∞' : numberWithCommas(maxSupply) below says if max supply value is null (for the current coin from the JSON) then set maxSupply to else set maxSupply var to numberWithCommas(maxSupply)

I got the numberWithCommas(...) function from https://stackoverflow.com/a/2901298/1309377 to help format the number with commas like you asked for.

I also switched to .append() instead of .html() otherwise you will just be writing over yourself.

$.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {

  for (var i = 0; i < data.length; i++) {
    var coin = data[i].id;
    var maxSupply = data[i].max_supply;
    maxSupply = maxSupply == null ? '∞' : numberWithCommas(maxSupply)
    $("#max_supply").append(coin + ": " + maxSupply + "<br/>");
  }

});

function numberWithCommas(x){
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>Max Supply</span>
<div id="max_supply"></div>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.