-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 Designs
  • 688
  • 1
  • 6
  • 22
  • This question doesn't seems like duplicate. Questioner specifically asked how to use Math.round() with MAP prototype. I believe it needs to be detached from questions about decimals. Here is my version, addition to K.K. Designs answer. Since Math.round gets the nearest integer, in this case only toFıxed() should be used with MAP. data = data.map((item) => (item.Points / item.Games).toFixed(1)); – esenkaya Aug 10 '23 at 22:42