<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Round floats to the nearest X Comments</title>
  <id>tag:opensoul.org,2008:/2007/7/18/round-floats-to-the-nearest-x/comments</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://opensoul.org/2007/7/18/round-floats-to-the-nearest-x/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/7/18/round-floats-to-the-nearest-x" rel="alternate" type="text/html"/>
  <updated>2007-12-07T04:07:36Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Dan Kubb</name>
    </author>
    <id>tag:opensoul.org,2007-07-18:3259:4407</id>
    <published>2007-12-07T04:07:36Z</published>
    <updated>2007-12-07T04:07:36Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/18/round-floats-to-the-nearest-x" rel="alternate" type="text/html"/>
    <title>Comment on 'Round floats to the nearest X' by Dan Kubb</title>
<content type="html">&lt;p&gt;What about:&lt;/p&gt;


&lt;pre&gt;
require 'bigdecimal'

class Float
  def round(to = 0)
    rounded = BigDecimal(self.to_s).round(to)
    to == 0 ? rounded.to_i : rounded
  end
end
&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Matt Jones</name>
    </author>
    <id>tag:opensoul.org,2007-07-18:3259:3260</id>
    <published>2007-07-18T19:08:22Z</published>
    <updated>2007-07-18T19:08:22Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/18/round-floats-to-the-nearest-x" rel="alternate" type="text/html"/>
    <title>Comment on 'Round floats to the nearest X' by Matt Jones</title>
<content type="html">&lt;p&gt;This is a little more complicated, but it correctly rounds
results:&lt;/p&gt;


&lt;pre&gt;
class Float
  alias_method :old_round, :round unless method_defined?(:old_round)
  def round(round_to = 1.0)
    scaled = self / round_to
    rounded = scaled.old_round * round_to
    rounded % 1 == 0 ? rounded.to_i : rounded
  end
end
&lt;/pre&gt;</content>  </entry>
</feed>
