<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: decimal code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 13 Oct 2008 18:18:41 GMT</pubDate>
    <description>DZone Snippets: decimal code</description>
    <item>
      <title>Floating point round off</title>
      <link>http://snippets.dzone.com/posts/show/6033</link>
      <description>Source: &lt;a href="http://www.hans-eric.com/code-samples/ruby-floating-point-round-off/"&gt;Ruby: Floating point round off&lt;/a&gt; [hans-eric.com]&lt;br /&gt;&lt;br /&gt;With a little monkey patching we can add custom round off methods to the Float class.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Float&lt;br /&gt;  def round_to(x)&lt;br /&gt;    (self * 10**x).round.to_f / 10**x&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def ceil_to(x)&lt;br /&gt;    (self * 10**x).ceil.to_f / 10**x&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def floor_to(x)&lt;br /&gt;    (self * 10**x).floor.to_f / 10**x&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Example usage:&lt;br /&gt;&lt;br /&gt;num = 138.249&lt;br /&gt;num.round_to(2)&lt;br /&gt;# =&gt; 138.25&lt;br /&gt;&lt;br /&gt;num.floor_to(2)&lt;br /&gt;# =&gt; 138.24&lt;br /&gt;&lt;br /&gt;num.round_to(-1)&lt;br /&gt;# =&gt; 140.0&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 06 Sep 2008 00:26:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6033</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Decimal to fraction in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/3636</link>
      <description>// Adds a 'to_fraction' method to Float. Eg. 0.5.to_fraction =&gt; [1,2]&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Float&lt;br /&gt;  def number_decimal_places&lt;br /&gt;    self.to_s.length-2&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def to_fraction&lt;br /&gt;    higher = 10**self.number_decimal_places&lt;br /&gt;    lower = self*higher&lt;br /&gt;&lt;br /&gt;    gcden = greatest_common_divisor(higher, lower)&lt;br /&gt;&lt;br /&gt;    return (lower/gcden).round, (higher/gcden).round&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;private&lt;br /&gt;&lt;br /&gt;  def greatest_common_divisor(a, b)&lt;br /&gt;     while a%b != 0&lt;br /&gt;       a,b = b.round,(a%b).round&lt;br /&gt;     end &lt;br /&gt;     return b&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 06 Mar 2007 15:21:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3636</guid>
      <author>shell (Michelle)</author>
    </item>
    <item>
      <title>bindec.scm</title>
      <link>http://snippets.dzone.com/posts/show/3479</link>
      <description>// Convert list of 1s and 0s back to base ten number.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;; Andrew Pennebaker&lt;br /&gt;; 5 Feb 2007&lt;br /&gt;; License: GPL&lt;br /&gt;; URL: http://snippets.dzone.com/posts/show/3479&lt;br /&gt;&lt;br /&gt;(define bin-&gt;dec&lt;br /&gt;	(lambda (b)&lt;br /&gt;		(cond&lt;br /&gt;			((integer? b) b)&lt;br /&gt;			((= (length b) 0) 0)&lt;br /&gt;			(else&lt;br /&gt;				(+&lt;br /&gt;					(* (expt 2 (- (length b) 1)) (car b))&lt;br /&gt;					(bin-&gt;dec (cdr b)))))))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Feb 2007 03:21:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3479</guid>
      <author>mcandre (Andrew Pennebaker)</author>
    </item>
    <item>
      <title>decbin.scm</title>
      <link>http://snippets.dzone.com/posts/show/3478</link>
      <description>// Converts a base ten integer to a list of 1s and 0s&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;; Andrew Pennebaker&lt;br /&gt;; 3 Feb 2007&lt;br /&gt;; License: GPL&lt;br /&gt;; URL: http://snippets.dzone.com/posts/show/3478&lt;br /&gt;&lt;br /&gt;(define dec-&gt;bin&lt;br /&gt;	(lambda (d)&lt;br /&gt;		(cond&lt;br /&gt;			((&lt; d 1) (list 0))&lt;br /&gt;			((= d 1) (list 1))&lt;br /&gt;			((&gt; d 1) (append&lt;br /&gt;				(dec-&gt;bin (floor (/ d 2)))&lt;br /&gt;				(list (if (= (modulo d 2) 0) 0 1)))))))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Feb 2007 03:18:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3478</guid>
      <author>mcandre (Andrew Pennebaker)</author>
    </item>
    <item>
      <title>Convert a decimal value to hex in Python</title>
      <link>http://snippets.dzone.com/posts/show/2270</link>
      <description>// Convert a dec value to hex &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;hexValue = "%X" % 256&lt;br /&gt;print hexValue  #100&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Jul 2006 19:12:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2270</guid>
      <author>kodreaming ()</author>
    </item>
    <item>
      <title>to-octal function</title>
      <link>http://snippets.dzone.com/posts/show/1112</link>
      <description>&lt;code&gt;&lt;br /&gt;    to-octal: func [&lt;br /&gt;        "Converts an integer to an octal issue!."&lt;br /&gt;        value [integer!] "Value to be converted"&lt;br /&gt;        /width wd&lt;br /&gt;        /local result pad&lt;br /&gt;    ][&lt;br /&gt;        pad: func [val wd] [head insert/dup val #"0" wd - length? val]&lt;br /&gt;        result: copy #   ; empty issue!&lt;br /&gt;        if 0 = value [return pad result any [wd 1]]&lt;br /&gt;        while [0 &lt;&gt; value] [&lt;br /&gt;            insert result value // 8&lt;br /&gt;            value: round/down value / 8&lt;br /&gt;        ]&lt;br /&gt;        pad result any [wd 1]&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 05:59:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1112</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>from-octal</title>
      <link>http://snippets.dzone.com/posts/show/1111</link>
      <description>&lt;code&gt;&lt;br /&gt;    from-octal: func [&lt;br /&gt;        "Converts an octal number to a decimal value."&lt;br /&gt;        octal [any-string! integer!]&lt;br /&gt;        /local result len&lt;br /&gt;    ][&lt;br /&gt;        result: 0&lt;br /&gt;        octal: form octal&lt;br /&gt;        repeat i len: length? octal [&lt;br /&gt;            result: add result (to integer! form octal/:i) * (8 ** (len - i))&lt;br /&gt;        ]&lt;br /&gt;        any [attempt [to integer! result] result]&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 05:58:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1111</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
