# Reading floating points into a field stored as Integers

**URL:** <https://www.ruby-forum.com/t/reading-floating-points-into-a-field-stored-as-integers/99447>\
**Category:** Rails\
**Created:** [July 22, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/reading-floating-points-into-a-field-stored-as-integers/99447 "2006-07-22T21:09:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [July 22, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/reading-floating-points-into-a-field-stored-as-integers/99447/1 "2006-07-22T21:09:44Z")

</div>

Hi, I’m trying to follow the “AWDR” example of storing certain types of  
numbers (in my case, percentages not money) as integers in the  
database. I understand from the Depot application how to format the  
output to have 2 places (or n places for that matter) decimal.

Now, I"m wondering about the input. It’s more natural for people to  
enter numbers as “11.23” if that’s what it means rather than 1123.  
What’s the correct way to fix this? Should I read the numbers as  
entered by the user and multiply by 100 and type cast to integer (ouch,  
my C programming is showing)

Thanks,  
mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [July 22, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/reading-floating-points-into-a-field-stored-as-integers/99447/2 "2006-07-22T21:26:18Z")

</div>

As of yesterday, the new beta version of AWDWR references the use of  
BigDecimal in Edge Rails, and says don’t bother with integers. So,  
if you’re just getting started, I would “rake rails:freeze:edge” and  
not bother with integers.

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [July 22, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/reading-floating-points-into-a-field-stored-as-integers/99447/3 "2006-07-22T21:37:27Z")

</div>

Yep, by using migrations, you’re trading some potential optimizations  
available from one kind of database for the ability to trivially  
switch between databases. I think worrying about those extra 10  
digits is likely a case of premature optimization, given the price of  
disk drives.

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [July 22, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/reading-floating-points-into-a-field-stored-as-integers/99447/4 "2006-07-22T21:30:56Z")

</div>

Thanks Dan!

That reminds me! I need to update my PDF! I’m not sure if the new  
version of AWDWR covers how BigDecimal can be used in migrations. My  
first feeling was that portable migrations are rather scant on data  
types 😑

For example, there is only one integer type which maps to int(11) on  
MySQL. I have 18 fields in my model that take a value between 1 and 5.  
It seems a bit of a waste. But, I’m just going along for now - will  
drop the tables, change the migrations and re-apply later when I am done  
with the first quick prototype to see where I’m heading.

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [July 22, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/reading-floating-points-into-a-field-stored-as-integers/99447/5 "2006-07-22T21:40:47Z")

</div>

I agree with you! I’ve made a note in my programming log that it can be  
optimized and has been marked for a later point in the development 🙂  
though I have to admit that it does bother me a bit since I come from a  
C and assembly language background in which 10 extra bytes x 18 fields =  
180 extra bytes per record would be considered career-ending 🙂

Cheers  
Mohit.
