-1

What I want: after calculation result should be in 2 decimal format.

here's my code

let totalNetWeightLocal = 0;

totalNetWeightLocal = totalNetWeightLocal + Number((parseFloat(item.netWeight) * parseInt(item.quantity)).toFixed(2));

Problem: calculation is working but toFixed() isn't working. I'm getting results in more than 2 decimal values.

Can someone please help me?

  • Can you give an example where it is not working? – Bharat Jul 30 '21 at 11:16
  • Your code works fine. Can you provide an example with an input that returns a wrong error? – AmD Jul 30 '21 at 11:18
  • 1
    In your case, the result would only have more than 2 decimal points, if the initial value of `totalNetWeightLocal` on your first line has more than 2 decimal points as well (and isn't `0` like in your example). – NullDev Jul 30 '21 at 11:18
  • Are you trying to have 2 decimal even if those 2 digits will be `.00` ? if so you may have a look here (https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places)[https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places] – Cedric Cholley Jul 30 '21 at 11:19
  • I think the problem is the type casting with `Number()`, which will remove from the string all the unnecessary zeros (check, for example `console.log(Number('50.00'))`, `console.log(Number('50.10'))` and `console.log(Number('50.11'))`) – secan Jul 30 '21 at 11:21
  • Given that `totalNetWeightLocal` is a number, it doesn't have a format. Do you want to make it a string? – Bergi Jul 30 '21 at 11:37

2 Answers2

1

The problem with your code is, that the .toFixed(2) is at the wrong position

What your code does is something like

const fullWeight = parseFloat(item.netWeight) * parseInt(item.quantity)
totalNetWeightLocal = totalNetWeightLocal + fullWeight.toFixed(2));

Which means, you add two strings together like 0 + '10.24' which will be 010.24. What you need to do is:

// you don’t need Number() here:
const itemWeight = parseFloat(item.netWeight) * parseInt(item.quantity)
totalNetWeightLocal += itemWeight;
totalNetWeightLocal.toFixed(2);

Considering, you might have a list of items you can write a functions as follows:

const items = [
  { netWeight: '4.53', quantity: '3' },
  { netWeight: '20.33', quantity: '10' }
];

const getTotalNetWeightLoal = items => {
  const totalNetWeightLocal = items.reduce(
    (weight, { netWeight, quantity }) =>
      weight + parseFloat(netWeight) * parseInt(quantity),
    0
  );

  return totalNetWeightLocal.toFixed(2);
};

console.log(getTotalNetWeightLoal(items));
Leif Marcus
  • 478
  • 4
  • 6
0

Try this code. Your code's working fine.

let item = { "netWeight": "5.99498", "quantity": '8' }
    let totalNetWeightLocal = 0;
    totalNetWeightLocal = totalNetWeightLocal + Number((parseFloat(item.netWeight) * parseInt(item.quantity)).toFixed(2));
    console.log(totalNetWeightLocal) // output: 47.96

//Can you provide cases where it failed?
Gere
  • 21
  • 2
  • This cannot work correctly as `toFixed(2)` is returning a string, wile the code above is returning a number value. this is due to the fact that `toFixed()` is used wrong here. – Leif Marcus Jul 30 '21 at 12:52
  • If the intent is to round the numbers, it should be done with `Math.round(totalNetWeightLocal * 100)/100` – Leif Marcus Jul 30 '21 at 12:53