1

I have an array of array

array=[[27536, 29675, 71974, 17187],
 [42972, 27113, 91087, 25581],
[53533, 36305, 110376, 38949],
[45201, 26403.1, 93274, 30133],
[45869, 31981.63, 85988, 21120],
[58265, 36418.2, 109869, 25167],
[0, 32140.37, 0, 0],
[0, 36124.11, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1],
[273376, 256160.41, 562568, 158137]];

and i am adding the values like

var result1=array.map(item => item.reduce((a, b)=> a + b, 0));

It is working fine but the output i am getting after this has many digits(last one) after .  

[146372, 186753, 239163, 195011.1, 184958.63, 229719.2, 32140.37, 36124.11, 0, 0, 0, 1, 1250241.4100000001]

I want the number to have 2 digits after .. I tried something like this but the values i am getting is in string.

var result=array.map(item => item.reduce((a, b)=> a + b, 0).toFixed(2));

Any suggestion how can i achieve the same as Numbers?

array=[[27536, 29675, 71974, 17187],
 [42972, 27113, 91087, 25581],
[53533, 36305, 110376, 38949],
[45201, 26403.1, 93274, 30133],
[45869, 31981.63, 85988, 21120],
[58265, 36418.2, 109869, 25167],
[0, 32140.37, 0, 0],
[0, 36124.11, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1],
[273376, 256160.41, 562568, 158137]];

console.log(array);
//Converting in string
var result=array.map(item => item.reduce((a, b)=> a + b, 0).toFixed(2));
console.log(result)
//Too many digits after "."
var result1=array.map(item => item.reduce((a, b)=> a + b, 0));
console.log(result1)
Learner
  • 55
  • 8
  • Binary floating-point numbers won't behave the way you want them to. – Pointy Oct 29 '19 at 13:35
  • Possible duplicate of [How to deal with floating point number precision in JavaScript?](https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – Peter B Oct 29 '19 at 13:41

2 Answers2

0

The return value of toFixed() is a string representing the given number using fixed-point notation.

You can try map(Number) on the result

array=[[27536, 29675, 71974, 17187],
 [42972, 27113, 91087, 25581],
[53533, 36305, 110376, 38949],
[45201, 26403.1, 93274, 30133],
[45869, 31981.63, 85988, 21120],
[58265, 36418.2, 109869, 25167],
[0, 32140.37, 0, 0],
[0, 36124.11, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1],
[273376, 256160.41, 562568, 158137]];


var result=array.map(item => item.reduce((a, b)=> a + b, 0).toFixed(2)).map(Number);
console.log(result);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • If converting a `toFixed()` result back to a number, the decimal rounding effect is lost for most numbers, because they are stored in a binary format. Example: `1.23`will be stored as `1.12999999999999989`. – Wiimm Oct 30 '19 at 13:03
0

You can use parseFloat to parse your string into a float again.

array=[[27536, 29675, 71974, 17187],
 [42972, 27113, 91087, 25581],
[53533, 36305, 110376, 38949],
[45201, 26403.1, 93274, 30133],
[45869, 31981.63, 85988, 21120],
[58265, 36418.2, 109869, 25167],
[0, 32140.37, 0, 0],
[0, 36124.11, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1],
[273376, 256160.41, 562568, 158137]];

var result=array.map(item => parseFloat(item.reduce((a, b)=> a + b, 0).toFixed(2)));
console.log(result)
Luïs
  • 2,671
  • 1
  • 20
  • 33
  • 1
    Same comment as above: If converting a `toFixed()` result back to a number, the decimal rounding effect is lost for most numbers, because they are stored in a binary format. Example: `1.23`will be stored as `1.12999999999999989`. – Wiimm Oct 30 '19 at 13:04