<?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>Jtanium's Notebook &#187; currency</title>
	<atom:link href="http://www.jtanium.com/tag/currency/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jtanium.com</link>
	<description>I jot things down, in hopes of finding them later...</description>
	<lastBuildDate>Wed, 21 Dec 2011 23:21:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Handling Currency in Rails</title>
		<link>http://www.jtanium.com/2009/10/17/handling-currency-in-rails/</link>
		<comments>http://www.jtanium.com/2009/10/17/handling-currency-in-rails/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 21:44:34 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[currency]]></category>
		<category><![CDATA[currency_magic]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/?p=158</guid>
		<description><![CDATA[So I recently ran into a situation where I had to start actually working with dollar amounts. I think that was pretty obvious given my last post about formatting currency in JavaScript. With the UI figured out, I was trying to deal with getting the amounts into the server. We all (hopefully) know you should [...]]]></description>
			<content:encoded><![CDATA[<p>So I recently ran into a situation where I had to start actually working with dollar amounts. I think that was pretty obvious given my last post about <a href="http://www.jtanium.com/2009/10/14/format-currency-in-javascript/">formatting currency in JavaScript</a>. With the UI figured out, I was trying to deal with getting the amounts into the server.</p>
<p>We all (hopefully) know you should always use integers to do your currency calculations.  But users like to have dollars and cents to work with. Somewhere is going to live some code that handles that conversion. One post I found gave the following solution: add a before_filter to go through and convert anything that looked like it might be a currency amount:</p>
<pre>
def filter_units (input)
 if [Array, Hash, HashWithIndifferentAccess].include?(input.class)
  input.each do |key, value|
   #recurse through the data structure
   input[key] = self.filter_units(value)
  end
 #match the string format for a major unit
 elsif not input.nil? and input.match(/^\d+\.\d\d)$/)
  #convert to a minor unit integer
  (input.to_f * 100.0).to_i
 else
  #return the value unchanged
  input
 end
end
</pre>
<p>No offense John, but that is a terrible way of dealing with it.  #1. I couldn&#8217;t even get the code to work, it gave all kinds of errors.  #2. What if you have another field the contains a float, e.g. tax_percent or weight_in_lbs, which aren&#8217;t currency, but could match the format above? #3. What about when a user decides to put in 9.5 instead of 9.50, or include a dollar sign themselves ($9.50)?  Finally, this still leaves you having to deal the conversion to a float by hand when it comes time to display the amount.</p>
<p>Here was my solution.  I just added some virtual attributes to my models:</p>
<pre>
class Subscription < ActiveRecord::Base
  def price_dollars
    (self.price / 100).round
  end
  def price_dollars=(price)
    self.price = (price.to_f * 100).to_i
  end
end
</pre>
<p>*Poof!* No cumbersome code!  But the above quickly becomes painful when you have several attributes, especially when they're spread across models.  So extracted the above into a plugin, which I, with a complete lack of creativity, named <a href="http://github.com/jtanium/currency_magic">CurrencyMagic</a>.</p>
<pre>
class Subscription < ActiveRecord::Base
  currency_magic :dollars, :price, :cost, :sales_price, :etc
end
</pre>
<p>Now, I'm not in love with the plugin idea, but I wanted to have a way to test the code and share it across projects, so that's what I did. I originally made it a module that could be included, so there's really nothing more than re-organizing the code for it to work outside ActiveRecord, though I imagine that would diminish it's value somewhat.</p>
<p>Anywho, there you go, my solution to converting dollars to cents and back in Rails.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2009/10/17/handling-currency-in-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Current Currency Conversion Rates from Yahoo!</title>
		<link>http://www.jtanium.com/2008/11/03/current-currency-conversion-rates-from-yahoo/</link>
		<comments>http://www.jtanium.com/2008/11/03/current-currency-conversion-rates-from-yahoo/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 21:18:50 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[currency]]></category>
		<category><![CDATA[stock quote]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=68</guid>
		<description><![CDATA[Have you ever needed current currency conversion rates? Well, Yahoo! has made it super easy, they the conversions like stock quotes. You just use the ISO currency codes as stock ticker. Check it, to get the conversion rate for US Dollar (USD) to Pound Sterling (GBP), use the &#8220;ticker&#8221; USDGBP=X. Combine that with being able [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever needed current currency conversion rates?  Well, Yahoo! has made it super easy, they the conversions like stock quotes.  You just use the ISO currency codes as stock ticker.  Check it, to get the conversion rate for US Dollar (USD) to Pound Sterling (GBP), use the &#8220;ticker&#8221; <a href="http://finance.yahoo.com/q?s=USDGBP=X">USDGBP=X</a>.</p>
<p>Combine that with being able to get quotes in CSV, and now you can programatically access current currency conversion rates.  For example, you could use the <a href="http://www.transparentech.com/opensource/yahoofinance">yahoofinance.rb</a>:</p>
<pre>ruby yahoofinance.rb USDGBP=X
Retrieving quotes for: USDGBP=X
QUOTING: USDGBP=X
YahooFinance::StandardQuote
volume = 0
ask = 0.6312
lastTradeWithTime = 3:56pm - <b>0.6311</b>
bid = 0.631
dayRange = N/A - N/A
changePercent = 0.0
date = 11/3/2008
tickerTrend = N/A
changePoints = 0.0
change = N/A - N/A
name = USD to GBP
open = 0.0
previousClose = 0.0
lastTrade = 0.6311
averageDailyVolume = 0
dayLow = 0.0
time = 3:56pm
dayHigh = 0.0
symbol = USDGBP=X</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2008/11/03/current-currency-conversion-rates-from-yahoo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

