# Convert to numeric

**URL:** <https://www.ruby-forum.com/t/convert-to-numeric/182287>\
**Category:** Rails\
**Created:** [January 26, 2010, 5:38am UTC](https://www.ruby-forum.com/t/convert-to-numeric/182287 "2010-01-26T05:38:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![saboor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@saboor](https://www.ruby-forum.com/u/saboor)\
**Post date:** [January 26, 2010, 5:38am UTC](https://www.ruby-forum.com/t/convert-to-numeric/182287/1 "2010-01-26T05:38:58Z")

</div>

hi, i am new user of ruby on rails  
i have a problem in casting a variable in to int , my code is  
@EmployeeTenure = Employeepersonaldetail.find\_by\_sql("select round  
(DATEDIFF("date1,date2)/365,0) from table where EmployeeID = 0001)

now if @EmployeeTenure is greater then 5 so make it 5

if (@EmployeeTenure .to\_d\>5)  
@EmployeeTenure=5

but there is an error so help me fixing the problem  
thanks

---

<div class="post-metadata">

**Author:** ![saboor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@saboor](https://www.ruby-forum.com/u/saboor)\
**Post date:** [January 26, 2010, 6:35am UTC](https://www.ruby-forum.com/t/convert-to-numeric/182287/2 "2010-01-26T06:35:28Z")

</div>

Saboor wrote:

> hi, i am new user of ruby on rails  
> i have a problem in casting a variable in to int , my code is  
> @EmployeeTenure = Employeepersonaldetail.find\_by\_sql("select round  
> (DATEDIFF("date1,date2)/365,0) from table where EmployeeID = 0001)
> 
> now if @EmployeeTenure is greater then 5 so make it 5
> 
> if (@EmployeeTenure .to\_d\>5)  
> @EmployeeTenure=5
> 
> but there is an error so help me fixing the problem  
> thanks

first at all… don’t capitalize a class variable! =\> @emplozeetenure is  
correct

2nd: i don’t remember that there is a to\_d method. use to\_f, because  
float has always double-precision in ruby

3rd: don’t use to\_f better use to\_i… if there compared number anyway  
is a Integer (Fixnum) then better to compare it with a Fixnum.
