Rounding a float to the nearest integer in ruby

If i have a float of 49.967 and I do .to_i it will chop it down to 49 which for my use of disk space analysis .967 is over 900mb of space that wont be accounted for in the displays.

Is there a function to round numbers to the nearest integer or would i have to define it my self like this:

class Float
  def to_nearest_i
    (self+0.5).to_i
  end
end

so that i could then do:

>> 5.44.to_nearest_i
=> 5
>> 5.54.to_nearest_i
=> 6

Answers


Try Float.round.

irb(main):001:0> 5.44.round
=> 5
irb(main):002:0> 5.54.round
=> 6

Need Your Help

Issues with multiple row deletion in Cassandra with java

java cassandra

I am having a Casandra DB modeled in such a way the the data_time will be the row Key. Row Key is in the format (yyyy_mm_dd_hh). This has been modeled as per the application needs.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.