if you have integer type fields than how can you save float value in it. On Mar 11, 10:28 am, Yudi Soesanto <[email protected]> wrote: > 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 viahttp://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]<rubyonrails-talk%[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.

