From: Raul Jara Date: 2010-01-23T11:20:04+09:00 Subject: Re: why is rails/ruby automatically rounding to whole number Morgan Morgan wrote: > Morgan Morgan wrote: >> @wtf= 751 / 750 >> >> returns 1 in the view >> >> @wtf= 3 / 750 >> >> returns 0 in the view >> >> @wtf= 749 / 750 >> >> returns 0 in the view. >> >> >> can somebody explain what's going on here. i'm just trying to calculate >> percents. > > > Apparently if you add a .0 it turns it into a precise number In ruby there are two kinds of numbers (really more than that, but let's just leave it at two for now) Fixnums and Floats. Fixnums are basically whole numbers or integers. Floats are any numbers with decimal places in them. Whenever you do math between two Fixnums, ruby assumes that you want a fixnum as an answer, and it will only provide a fixnum as a result. This is incredibly useful when working with arrays, because if an array is length 9 and you divide that by 2, you get 4, which is a usable index for an array. 4.5 wouldn't be. However if any math operation involve one number that is a float, the answer will be a float. So 1 (Fixnum) / 2 (Fixnum) = 0 (Fixnum). But 1.0 (Float) / 2 (Fixnum) = 0.5 (Float). Is that clear? -- Posted via http://www.ruby-forum.com/.