<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Ruby on Rails Notes &#187; integer</title> <atom:link href="http://railsnotes.com/tag/integer/feed/" rel="self" type="application/rss+xml" /><link>http://railsnotes.com</link> <description>A code-heavy brain dump of stuff I come across working on Ruby on Rails projects including Models, ActiveRecord, Views, Controllers, RESTful rails, deployment, server stuff, etc.</description> <lastBuildDate>Sun, 29 Aug 2010 23:45:16 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Integer to Floating Point in Ruby</title><link>http://railsnotes.com/84-integer-to-floating-point-in-ruby/</link> <comments>http://railsnotes.com/84-integer-to-floating-point-in-ruby/#comments</comments> <pubDate>Fri, 06 Feb 2009 02:34:29 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[Ruby & Rails Core]]></category> <category><![CDATA[fixnum]]></category> <category><![CDATA[float]]></category> <category><![CDATA[integer]]></category> <category><![CDATA[ruby]]></category><guid
isPermaLink="false">http://railsnotes.wordpress.com/?p=84</guid> <description><![CDATA[Okay, this is something weird I ran into today. If you do 3/4 in ruby, it will give you back 0. Basically it has to do with the fact that both are integers. If you want to divide two numbers and get back a decimal number (aka floating point number), you&#8217;ll need at least one [...]]]></description> <content:encoded><![CDATA[<p></p><p>Okay, this is something weird I ran into today. If you do 3/4 in ruby, it will give you back 0. Basically it has to do with the fact that both are integers.</p><p>If you want to divide two numbers and get back a decimal number (aka floating point number), you&#8217;ll need at least one of them to be a floating point.</p><p>How do you turn an integer into a floating point? Use .to_f or * 1.0 or =+ 0.0</p><p>Taking the above example, 3/4.to_f =&gt; 0.75</p><p>Now, if you want to limit how many numbers show up after the decimal point, you&#8217;ll need to use the sprintf function. Here are a few examples:</p><p>===============</p><p>&gt;&gt; sprintf(&#8216;%0.1f&#8217;, (3.to_f/8))</p><p>=&gt; &#8220;0.4&#8243;</p><p>&gt;&gt; sprintf(&#8216;%0.2f&#8217;, (3.to_f/8))</p><p>=&gt; &#8220;0.38&#8243;</p><p>&gt;&gt; sprintf(&#8216;%0.3f&#8217;, (3.to_f/8))</p><p>=&gt; &#8220;0.375&#8243;</p><p>&gt;&gt; sprintf(&#8216;%0.4f&#8217;, (3.to_f/8))</p><p>=&gt; &#8220;0.3750&#8243;</p><p>===================</p><p>Now, if you need to format numbers instead a view, you should check out the number_to &amp; number_with helpers.</p><p>Thanks to <a
href="http://flow.handle.it/" target="_blank">Jason King</a> for telling me about all this.</p> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/84-integer-to-floating-point-in-ruby/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 4/16 queries in 0.044 seconds using disk

Served from: railsnotes.com @ 2010-09-10 05:09:29 -->