Keep zeroes after the point

Asked

Viewed 58 times

0

Good morning to you all. I have the following code:

var somaR = 0;
    var somaN = 0;
    var project = [
       {part: 'Renault', partNumber: 1234, project: 'X19', gap: 15.000}, 
       {part: 'Renault', partNumber: 1234, project: 'X19', gap: 600}, 

    ]

    function groupBy(array, f) {
        var groups = {};
        array.forEach(function(o) {
            var group = JSON.stringify(f(o));
            groups[group] = groups[group] || [];
            groups[group].push(o);
        });
        return Object.keys(groups).map(function(group) {
            return groups[group];
        });
    }

    var projectIguais = groupBy(project, function(item) {
        return [item.part, item.partNumber, item.project];
    }).map(function(data){
        return {
            totalGap: data.reduce(function(total, valor){return total + valor.gap;}, 0),
            itens: data
        };
    });

    console.log(projectIguais);


    for (var i in projectIguais) {

        var partNumberItem = "Part Number: " +projectIguais[i].itens[0].partNumber;
        var projectItem = "Project: " +projectIguais[i].itens[0].project;
        var gap = "Gap: "+projectIguais[i].itens[0].gap;
        var totalGapItem = projectIguais[i].totalGap;

        console.log(partNumberItem);
        console.log(projectItem);
        console.log(gap);

        if(projectIguais[i].itens[0].part == 'Renault'){
            somaR += totalGapItem;
        }
        if(projectIguais[i].itens[0].part == 'Nissan'){
            somaN += totalGapItem;
        }
    }

    console.log("Soma Renault", somaR);
    console.log("Soma Nissan", somaN);

Inside the project array I have the gap 15,000 but when the calculation is done, the gap value is 15.

Exp: The code takes inside the array the same partNumber and project, sums the gap and returns the total of the gap per project. In my array I have a gap 15,000 and another gap 600 but in the calculation it is returning me gap 615 because it is removing the zeroes after the point.

How do I keep the 15,000 in the calculation?

  • Json is wrong, the correct one for numerical json in thousands is to receive without any point, Exp: 15000, the . separates the fraction, Exp: 15000.58 => 15.000,58

1 answer

1

Quick response

Write:

{part: 'Renault', partNumber: 1234, project: 'X19', gap: 15000}, 

In place of:

{part: 'Renault', partNumber: 1234, project: 'X19', gap: 15.000}, 

And when it comes to showing the value, then yes you put the dots and commas to separate thousand and decimal:

// .toLocaleString('pt-BR') vai aplicar o formato brasileiro no Number
// ou seja, um ponto var separar o milhar e uma vírgula vai separar o decimal
var gap = "Gap: " + (projectIguais[i].itens[0].gap).toLocaleString('pt-BR');

// Use em todas as saídas que quiser formatar
console.log("Soma Nissan", (somaN).toLocaleString('pt-BR'));

Explanation of all this

The point "." , in the American notation (which javascript uses) is understood as our comma here in Brazil, that is, it separates the whole number from the decimal places, so writing R$ 15,000 is equal to writing R$ 15,000 (we read it as "fifteen real", we always ignore the decimal places when they are ZEROED).

Completion

To work with the numbers in javascript do not use any punctuation to separate the thousands and use the point (not the comma) to separate the decimals, simple like this.

Bonus

Create a function to facilitate formatting (and your life):

function ptbr(n) {
    // parse só pra garantir
    return Number(n).toLocaleString('pt-BR')
}

That’s how you use it:

var gap = "Gap: " + ptbr(projectIguais[i].itens[0].gap);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.