On the page, for example, there is such code:

<span class="cur-btc">100</span> BTC 

The number 100 I need to multiply by the value of json, multiply by the usual number and round up to 3 decimal places. How to make one function?

The value of json I will receive here:

 https://blockchain.info/tobtc?currency=USD&value=1 

    1 answer 1

    Excellent TK))) Like this:

     const roundPlus = (x, n) => { //x - число, n - количество знаков if (isNaN(x) || isNaN(n)) return false; var m = Math.pow(10, n); return Math.round(x * m) / m; } const f = () => { $.ajax({ url: "https://blockchain.info/tobtc?currency=USD&value=1", dataType: "html" }).done(response => { let prostoeChislo = 5; //простое число let inTag = Number($("span.cur-btc").html()); let inResponse = Number(response); let result = roundPlus((inTag * inResponse * prostoeChislo), 3); console.log("(В теге: " + inTag + ")", "(В json: " + inResponse + ")", "(Простое число: " + prostoeChislo + ")"); console.log("Результат перемножения и округления: " + result); $("span.cur-btc").html(result); }); } f(); 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="cur-btc">100</span> BTC 

    • And if you need to put the result from where it took, that is, replace the number 100? Thanks for the quick response) - Dmitry
    • Instead of 100 inside the span ? - Misha Saidov
    • Yes, replace with the result - Dmitriy
    • html(value) . Edited the answer. - Misha Saidov
    • Almost, only this script works with only one tag, and I may have several of them. I got a slightly different solution, but without multiplying my number: (async function () {const response = await fetch (" blockchain.info/tobtc?currency=USD&value=1" ); const value = await response.json (); for (const element of document.querySelectorAll (". cur-btc")) {element.textContent = (Number (element.textContent) * value) .toFixed (3);}}) (); - Dmitriy