1

Using a crypto API I'm receiving the top currencies in the last 24 hours and their price in bitcoin. I want to round that price within my for loop. So Im trying to Round the "+result.coins[i].item.price_btc+" value

This number here is way to long and I want to round it [1]: https://i.stack.imgur.com/IxJfR.png

const coinTrending = "https://api.coingecko.com/api/v3/search/trending";

    $.getJSON(coinTrending, function(result){
    
        for(var i = 0; i < result.coins.length; i++){
            console.log("Local Time");

            //variable for html
            var TopCur = `
            <div class="p-2 h-full bg-gray-900 py-2 px-2 grid md:grid-cols-2 grid-cols-4 text-xs justify-items-center border-2 border-y-white">
                    <div class="text-center text-white text-base">`+result.coins[i].item.name+`</div>
                    <img src="`+result.coins[i].item.small+`" class="w-7 h-7 bg-white rounded-full"></img>
                    <div class="py-2 text-white">Market Cap:</div>
                    <div class="py-2 text-white">`+result.coins[i].item.market_cap_rank+`</div>
                    <div class="py-2 text-white">Bitcoin Price</div>
                    <div class="py-2 text-white">`+result.coins[i].item.price_btc+`</div>
            </div>
            `;
            //add html to dom
            $(".TopCurrencies").append(TopCur);
        
        };
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  • 2
    *I want to round it* perfect method for that is .... `Math.round` or `Number.prototype.toFixed` – Bravo Mar 01 '22 at 12:26

1 Answers1

1

I'd suggest creating a roundPrice() function, wrapping Number.toFixed() perhaps. You can tweak this as you like to get the right balance:

const coinTrending = "https://api.coingecko.com/api/v3/search/trending";

$.getJSON(coinTrending, function(result){

    for(var i = 0; i < result.coins.length; i++){
        //variable for html
        var TopCur = `
        <div class="p-2 h-full bg-gray-900 py-2 px-2 grid md:grid-cols-2 grid-cols-4 text-xs justify-items-center border-2 border-y-white">
                <div class="text-center text-white text-base">`+result.coins[i].item.name+`</div>
                <img src="`+result.coins[i].item.small+`" class="w-7 h-7 bg-white rounded-full"></img>
                <div class="py-2 text-white">Market Cap:</div>
                <div class="py-2 text-white">`+result.coins[i].item.market_cap_rank+`</div>
                <div class="py-2 text-white">Bitcoin Price</div>
                <div class="py-2 text-white">` + roundPrice(result.coins[i].item.price_btc) + `</div>
        </div>
        `;
        //add html to dom
        $(".TopCurrencies").append(TopCur);
        $(".TopCurrencies").append('<br>');
    
    };
});

function roundPrice(price) {
    return price.toFixed(8)
}
<div class='TopCurrencies'>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40