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

409
Visualizações
How can I use this toFixed(2) in these codes?

It already shows the correct percentage. However, this is what it looks like:

8.333333333333332

How can I make it display like this: 8.33%

Putting it inside this will cause an error:

acc[key] = (acc[key] || 0) + (1 / totalUsers) * 100.toFixed(2);

Below are the codes:

const movies = data.filter(v => v.genre?.selected == "Movie");
const counts = movies.reduce((acc, cur) => {
    return !cur["1"]?.drama
    ? acc:
    Object.entries(cur["1"].drama).reduce((acc, [key,value]) => { 
        if (value) { 
            acc[key] = (acc[key] || 0) + (1 / totalUsers) * 100;
        }
        return acc;
    }, acc)
}, {})

This is a part of the data:

 const totalUsers = 12

const data = [{
  "1": { drama: { Comedy: true, Romance: true, BuddyComedy: false } },
  "2": {
    drama2: { Tragedy: true},
    others: "Dystopian",
  },
  id: "zaMR9TR7hNV3p3VFNumyNbXMto93",
  genre: {
    selected: "Movie",
  },
  displayName: "p1"
}, 
 {
  "1": { drama: { Tragedy: true, Comedy : true} },
  "2": {
    drama2: { Romance: true}
  },
  id: "zaMR9TR7hNV3p3VFNumyNbXMto93", //sample id
  genre: {
    selected: "Movie",
  },
  displayName: "p2"
}]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You have to wrap the 100 in brackets.

If you don't, the interpreter believes the number is a decimal, and tries to parse the segment after the period ("toFixed(2)") as a number, causing an error.


In your case...

You'll have to parse the number using the Number constructor, then call toFixed():

Number(+(acc[key] || 0) + (1 / totalUsers) * (100)).toFixed(2);

Take note of the unary plus operator before "(acc[key] || 0)" used to parse it into a number, to ensure that we aren't concatenating strings. (if you don't add it, you'll end up with NaN)

Working code:

const totalUsers = 12

const data=[{1:{drama:{Comedy:!0,Romance:!0,BuddyComedy:!1}},2:{drama2:{Tragedy:!0},others:"Dystopian"},id:"zaMR9TR7hNV3p3VFNumyNbXMto93",genre:{selected:"Movie"},displayName:"p1"},{1:{drama:{Tragedy:!0,Comedy:!0}},2:{drama2:{Romance:!0}},id:"zaMR9TR7hNV3p3VFNumyNbXMto93",genre:{selected:"Movie"},displayName:"p2"}];

const movies = data.filter(v => v.genre?.selected == "Movie");
const counts = movies.reduce((acc, cur) => {
    return !cur["1"]?.drama ?
        acc :
        Object.entries(cur["1"].drama).reduce((acc, [key, value]) => {
            if (value) {
                acc[key] = Number(+(acc[key] || 0) + (1 / totalUsers) * (100)).toFixed(2);
            }
            return acc;
        }, acc)
}, {})


console.log(JSON.stringify(counts,0, 2))

about 4 years ago · Juan Pablo Isaza Relatório

0

You loose precision if you repeatedly round the number and parse it throughout the calculation. To preserve the precision of your numbers as much as possible, rounding should only be done once after all calculation completed.

const totalUsers = 12

const data=[{1:{drama:{Comedy:!0,Romance:!0,BuddyComedy:!1}},2:{drama2:{Tragedy:!0},others:"Dystopian"},id:"zaMR9TR7hNV3p3VFNumyNbXMto93",genre:{selected:"Movie"},displayName:"p1"},{1:{drama:{Tragedy:!0,Comedy:!0}},2:{drama2:{Romance:!0}},id:"zaMR9TR7hNV3p3VFNumyNbXMto93",genre:{selected:"Movie"},displayName:"p2"}];

const movies = data.filter(v => v.genre?.selected == "Movie");
const counts = movies.reduce((acc, cur) => {
    return !cur["1"]?.drama ?
        acc :
        Object.entries(cur["1"].drama).reduce((acc, [key, value]) => {
            if (value) {
                acc[key] = (acc[key] || 0) + (1 / totalUsers) * 100;
            }
            return acc;
        }, acc)
}, {})

const rounded = Object.entries(counts).reduce((acc,[key,value])=>{
  return {...acc,[key]:value.toFixed(2)};
},{});

console.log(rounded);

about 4 years ago · Juan Pablo Isaza Relatório

0

acc[key] = ((acc[key] || 0) + (1 / totalUsers) * 100).toFixed(2);

Wrap the whole thing in brackets and then put .toFixed(2)

const movies = data.filter(v => v.genre?.selected == "Movie");
const counts = movies.reduce((acc, cur) => {
    return !cur["1"]?.sideEffects1
    ? acc:
    Object.entries(cur["1"].drama).reduce((acc, [key,value]) => { 
        if (value) { 
            acc[key] = ((acc[key] || 0) + (1 / totalUsers) * 100).toFixed(2);
        }
        return acc;
    }, acc)
}, {})

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