-2

data.map(item => item.Points = item.Points / item.Games);

so i am pulling this data from an api then i am dividing the 2 numbers and i want to round it to the nearest first decimal after because it comes out as 27.77777777 i want it to just be 27.8

  • Does this answer your question (just change to 1 decimal for all the answers)? [How to round to at most 2 decimal places, if necessary?](https://stackoverflow.com/questions/11832914/how-to-round-to-at-most-2-decimal-places-if-necessary) – Samathingamajig Jan 13 '22 at 23:02

1 Answers1

0

This is quite easy to do. To preserve one decimal, you can use something like Math.round(num * 10) / 10. After adding this to you map function, you would get something like this:

data.map(item => item.Points = Math.round(item.Points / item.Games * 10) / 10) / 10));

Here's an example by using dummy numbers:

console.log(Math.round(25 / 4 * 10) / 10);

Hoped this helped!

K.K Desgins
  • 593
  • 1
  • 4
  • 19