Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
Adding decimal separators to a YouTube view count retrieved via API

I'm trying to retrieve a YouTube view count for a specific video on my Wordpress site. I'm not a coder but I've managed to get it working using this code:

<div id="viewCount" style="display: inline-block;"></div>
 <script>
       let getviewCount = () => {
        fetch(`https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Cemk32wKN_k&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX`)
        .then(response => {
            return response.json()
        })
        .then(data => {
            console.log(data);
            viewCount.innerHTML = data["items"][0].statistics.viewCount;
        })      
    }
    getviewCount(); 
    </script>

The final touch I'd like to add is decimal separators so instead of the number looking like this:

13526897

It looks like this:

13,526,897

Based on some research here I think I need to use a function similar to this:

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

But I don't know how to code, so combining these two ideas is beyond my ability right now.

If anyone wouldn't mind showing me how to do it, I'd be extremely grateful!

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Javascript has built-in functions for this

Intl.NumberFormat() function

It will format the number depending on the user's default browser language.

var number = 1234567890;
console.log(new Intl.NumberFormat().format(number));

Will output

1,234,567,890 for US clients

1.234.567.890 for German clients

about 4 years ago · Juan Pablo Isaza Relatório

0

let num = '13,526,897';
    num = num.replace(/\,/g, ''); 
    num = parseInt(num, 10);

You can create a function if you want to use this piece of code again:

function parseNumber(number) {
   return parseInt(number.replace(/\,/g,''), 10); 
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Congrats on your first venture into programming! copy and paste the function definition for numberWithCommas before the word let.

To use the function you added, simply call numberWithCommas with data["items"][0].statistics.viewCount inside the parenthesis.

In all, your new code would look like the following:

<div id="viewCount" style="display: inline-block;"></div>
 <script>
function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
       let getviewCount = () => {
        fetch(`https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Cemk32wKN_k&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX`)
        .then(response => {
            return response.json()
        })
        .then(data => {
            console.log(data);
            viewCount.innerHTML = numberWithCommas(data["items"][0].statistics.viewCount);
        })      
    }
    getviewCount(); 
    </script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda