I believe you have the solution in the previous answer.
When you do this division:
> @average_stat.points = stats.sum('points') / stats.count('points')
you divide two integers and ruby gives you an integer. That's the way it is.
so instead do this division : (as Robert suggested)
@average_stat.points = stats.sum('points').to_f / stats.count('points').to_f
Then the next question is what's gonna append if you try to save @average_stat
as this object doesn't carry integers anymore
Hope this helps,
Le 11 mars 2010 à 06:28, Yudi Soesanto a écrit :
> Here is my situation,
>
> In the database, I have a stats table which has:
> - points (Integer datatype)
> - assists (Integer datatype)
> - blocks (Integer datatype)
> - turnovers (Integer datatype)
>
> In rails, I have stat model
>
> Now If I want to display value from stats table, I can do this
> @stats = Stat.find(:all)
> It will display all integer value that user entered (Note: User ONLY allowed
> to enter integer value)
>
> For example, I have these data in stats table
> Points assists blocks turnovers
> 1 4 5 1
> 5 2 5 5
> 7 3 2 7
>
> Now, I want to average each column and assign it to stat class
>
> @average_stat = Stat.new
> stats = Stat.find(:all)
>
> @average_stat.points = stats.sum('points') / stats.count('points')
> @average_stat.assists = stats.sum('assists') / stats.count('assists')
> @average_stat.assists = stats.sum('blocks') / stats.count('blocks')
> @average_stat.assists = stats.sum('turnovers') / stats.count('turnovers')
>
> The problem I see, when I assigning to average value to @average_stat, the
> value get converted to integer. I want to have float value. How I can assign
> float value in stat class?
>
>
> Yudi Soesanto
>
>
> On Wed, Mar 10, 2010 at 11:07 PM, Robert Walker <[email protected]> wrote:
> Yudi Soesanto wrote:
> > How i can store integer data type and when application display the data
> > back
> > (Since it will do calculation) it will display float on each column
> > points,
> > assists, blocks and turnovers?
>
> This seems to be a basic Ruby question. See the following IRB session:
> >> x = 25
> => 25
> >> y = 3
> => 3
> >> z = x / y
> => 8
> >> z = x.to_f / y.to_f
> => 8.33333333333333
> >> puts "%0.3f" % z
> 8.333
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.