# Data Type Question

**URL:** <https://www.ruby-forum.com/t/data-type-question/184906>\
**Category:** Rails\
**Created:** [March 10, 2010, 8:50am UTC](https://www.ruby-forum.com/t/data-type-question/184906 "2010-03-10T08:50:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 10, 2010, 8:50am UTC](https://www.ruby-forum.com/t/data-type-question/184906/1 "2010-03-10T08:50:48Z")

</div>

Hi guys,

I have this situation,

#migration data  
class CreateStats \< ActiveRecord::Migration  
def self.up  
create\_table :stats do |t|  
t.integer :points  
t.integer :assists  
t.integer :blocks  
t.integer :turnovers  
end

def self.down  
drop\_table :stats  
end  
end

#Model  
class Stat \< ActiveRecord:Base

end

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?

Thanks for your help  
ys

---

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 10, 2010, 10:36am UTC](https://www.ruby-forum.com/t/data-type-question/184906/2 "2010-03-10T10:36:09Z")

</div>

On 10 March 2010 07:49, Yudi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> t.integer :blocks
> 
> ```
> 
> end
> 
> 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?

I think you need to give more detail of what you are attempting to  
achieve, give an example.

Colin

---

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 10, 2010, 5:07pm UTC](https://www.ruby-forum.com/t/data-type-question/184906/3 "2010-03-10T17:07:28Z")

</div>

Yudi S. 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

---

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 11, 2010, 6:30am UTC](https://www.ruby-forum.com/t/data-type-question/184906/4 "2010-03-11T06:30:15Z")

</div>

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 S.

---

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 11, 2010, 12:05pm UTC](https://www.ruby-forum.com/t/data-type-question/184906/5 "2010-03-11T12:05:32Z")

</div>

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 S. a Ã©crit :

---

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 11, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/data-type-question/184906/6 "2010-03-11T12:26:37Z")

</div>

If @average\_stat is Stat.new then @average\_stat.points,  
@average\_stat.assists, etc… are also integer. So you could use a  
array variable, after stats = Stat.find(:all) :  
@average\_stat=[ stats.sum(‘points’) / stats.count(‘points’),  
stats.sum(‘assists’) / stats.count(‘assists’) , stats.sum(‘blocks’) /  
stats.count(‘blocks’) , stats.sum(‘turnovers’) /  
stats.count(‘turnovers’) ] .  
In fact you shoud use directly average(stat.points), etc… See  
[http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M002188](http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M002188)

---

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 12, 2010, 11:05pm UTC](https://www.ruby-forum.com/t/data-type-question/184906/7 "2010-03-12T23:05:58Z")

</div>

Thanks for your help guys.

Yudi  
Sent from my AXIS Worry Free BlackBerry® smartphone

---

<div class="post-metadata">

**Author:** ![soesan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@soesan01](https://www.ruby-forum.com/u/soesan01)\
**Post date:** [March 11, 2010, 7:14pm UTC](https://www.ruby-forum.com/t/data-type-question/184906/8 "2010-03-11T19:14:10Z")

</div>

if you have integer type fields than how can you save float value in  
it.
