Home > Software design >  When i try to add results of average operation to an array, i get strange output
When i try to add results of average operation to an array, i get strange output

Time:01-27

I have three models, directors, movies, and ratings. These are their relations: A director has_many movies, a movie belongs_to a director, a movie has_many ratings and a rating belongs_to a movie.

I'm currently trying to display a director's best-rated movie to display on a _director.html.erb patrial, so I tried this code:

<% array = [] %>
<% rat_val = 0 %>
  <% director.movies.each do |movie| %>
     <% if movie.ratings.count > 0 %>
        <% director.movies.each do |movie| %>
           <% rat_val = movie.ratings.average(:value).round(2) %>
           <% array << rat_val %>
        <% end %>
        <%= array %>
        <% break %>
      <% end %>
   <% end %>

to get the overall rating of a movie as a single value to an array, so I could then take the biggest value, then find a director's movie with the same rating and display its name and the rating itself, but with <% array %> the output is [0.475e1, 0.438e1], which has the correct numbers, but somehow the decimal point is higher than it should be, the correct results are 4.75 and 4.38. Also, I have no idea what e1 means, if anyone could enlighten me, I'd be grateful.

CodePudding user response:

0.475e1 is 4.75

Type it into your rails (or plain ruby) console and see.

The e1 is part of scientific notation or e-notation. 0.123e2 stands for 12.3

0.123 * 10**2
0.123 * 100
12.3

It's a display issue.

  •  Tags:  
  • Related