Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
Rounding number inside variable from API

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>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda