<?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/"
	xmlns:series="https://publishpress.com/"
	>

<channel>
	<title>c# money Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist</title>
	<atom:link href="https://www.anujvarma.com/tag/c-money/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.anujvarma.com/tag/c-money/</link>
	<description>Production Grade Technical Solutions &#124; Data Encryption and Public Cloud Expert</description>
	<lastBuildDate>Thu, 29 Jun 2017 19:22:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.anujvarma.com/wp-content/uploads/anujtech.png</url>
	<title>c# money Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist</title>
	<link>https://www.anujvarma.com/tag/c-money/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Floats and Doubles can lose you money&#8230;use Decimals or a custom Money class instead</title>
		<link>https://www.anujvarma.com/floats-and-doubles-can-lose-you-moneyuse-decimals-instead/</link>
					<comments>https://www.anujvarma.com/floats-and-doubles-can-lose-you-moneyuse-decimals-instead/#respond</comments>
		
		<dc:creator><![CDATA[Anuj Varma]]></dc:creator>
		<pubDate>Tue, 18 Oct 2011 23:44:49 +0000</pubDate>
				<category><![CDATA[C# - the language]]></category>
		<category><![CDATA[currency money conversion precision c#]]></category>
		<category><![CDATA[.net currency]]></category>
		<category><![CDATA[c# money]]></category>
		<guid isPermaLink="false">http://www.anujvarma.com/floats-and-doubles-can-lose-you-moneyuse-decimals-instead/</guid>

					<description><![CDATA[<p>Ariane 5 Rocket Launcher Crash In 1996, the European Space Agency lost a valuable shuttle just 37 seconds into the launch sequence. After a deep dive into the root cause [&#8230;]</p>
<p>The post <a href="https://www.anujvarma.com/floats-and-doubles-can-lose-you-moneyuse-decimals-instead/">Floats and Doubles can lose you money&hellip;use Decimals or a custom Money class instead</a> appeared first on <a href="https://www.anujvarma.com">Anuj Varma, Hands-On Technology Architect, Clean Air Activist</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h3>Ariane 5 Rocket Launcher Crash</h3>
<p>In 1996, the European Space Agency lost a valuable shuttle just 37 seconds into the launch sequence. After a deep dive into the root cause analysis, here is a direct excerpt from their report:</p>
<p><cite>The internal SRI* software exception was caused during execution of a data conversion from 64-bit floating point to 16-bit signed integer value. The floating point number which was converted had a value greater than what could be represented by a 16-bit signed integer. </cite> </p>
<p><cite></cite>Basically, the navigation software of the rocket malfunctioned due to a simple conversion error – a concept that is taught in undergrad programming classes. </p>
<p>While not of the same caliber, currency conversion within your software can cause the same TYPE of problems – related in effect, to the ‘type’ of the storage variable used.&nbsp; We all know that trying to store Decimals as integers is a bad idea – for e.g. storing $10.3 as $10 – would lose 30 cents on every single transaction. In the same manner, storing any currency in the FLOAT type (which is what ALL programmers tend to start with), is a mistake. The DECIMAL class was invented for just this purpose. As this article will demonstrate, even that though, is not always sufficient.</p>
<h3>As a first step, when dealing with Currencies, use the Decimal type </h3>
<p>Floats are imprecise. Doubles are more precise than floats – but still not completely precise. Which means that if you start using floats (or doubles) to handle currency types, chances are you are losing precision – and therefore losing money. Here is a simple example:</p>
<p>Say you have a $100 which you have to divide 3 ways. You end up with $33.33&nbsp; a piece. If you multiply this again by 3 – you do not end up with your $100. You just lost 0.01 cents because of your conversion. You could argue that – <em>Let us keep more decimal places. </em>So, instead of 33.33, we have 33.33333333. </p>
<p>There are two problems here:</p>
<ol>
<li>The number of decimal places you specify cannot be unlimited.</li>
<li>Even if you specify a LOT of decimal places, you will still lose money when dealing with larger starting amounts (instead of $100, if we started with $100,000 for example).</li>
</ol>
<p>The problem above is exactly what computers face when trying to store any number. A float is only an approximate representation of a number. A double, though better than a float, is still approximate. To get around this problem, C# (.NET) defines a <em>Decimal </em>type. The</p>
<pre class="code"><font size="2"><span style="color: #2b91af">Decimal </span>d = 100.00M; // The M is what tells us that this is a ‘literal’ type – which is different from double, float</font></pre>
<pre class="code"><span style="color: #2b91af"><font size="2">// divide by 3</font></span></pre>
<pre class="code"><span style="color: #2b91af"></span><font size="2"><span style="color: #2b91af">Decimal </span>div = d / 3;</font></pre>
<pre class="code"><span style="color: #2b91af"><font size="2">// multiply by 3 (should get back 100.00 or as close as possible)</font></span></pre>
<pre class="code"><font size="2"><span style="color: #2b91af">Decimal </span>product = div * d;</font></pre>
<h3>As a second step, use a special <em>Money </em>type (Full Source Code available at end of article)</h3>
<p>The Decimal solution is better, but not perfect. There is still some loss of precision.&nbsp; Ideally, one should implement the <em>Money </em>pattern (as described in <a href="http://martinfowler.com/books.html#eaa">this</a> book). Just such an implementation can be found <a href="http://www.noticeablydifferent.com/CodeSamples/Money.aspx">here</a>.&nbsp; Using this <em>Money </em>class, all a client needs to do is:</p>
<p><font size="2"><span style="color: #2b91af">Money </span>usDollars = <span style="color: blue">new </span><span style="color: #2b91af">Money</span>(100m, <span style="color: #a31515">&#8220;en-us&#8221;</span>);<br /><span style="color: #2b91af">Money </span>convertedCanadianDollars = <span style="color: blue">new </span><span style="color: #2b91af">Money</span>(usDollars.Amount * 1.12565m, <span style="color: #a31515">&#8220;en-CA&#8221;</span>);</font></p>
<h3>Summary</h3>
<p>We should end up with a number as close to our starting number as possible with the use&nbsp; <em>Decimal.</em>&nbsp; However, even a <em>Decimal </em>isn’t perfect. The best solution is to use the <em>Money </em>class ( a special class designed to handle currency representation and conversion). The <em>Money </em>class will ensure as close to 100% precision as possible when dealing with currency applications. </p>
<p>If only the Ariane 5 could have it’s navigation module code rewritten to include a ‘Latitude’ and a ‘Longitude’ class….</p>
<div id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:26acc072-f43d-4313-83bf-6c54169f8da6" class="wlWriterSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">
<p><a href="http://www.anujvarma.com/wp-content/uploads/Currency8.zip" target="_blank">Download Currency Solution</a></p>
</div>
<p>The post <a href="https://www.anujvarma.com/floats-and-doubles-can-lose-you-moneyuse-decimals-instead/">Floats and Doubles can lose you money&hellip;use Decimals or a custom Money class instead</a> appeared first on <a href="https://www.anujvarma.com">Anuj Varma, Hands-On Technology Architect, Clean Air Activist</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.anujvarma.com/floats-and-doubles-can-lose-you-moneyuse-decimals-instead/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Content Delivery Network via N/A
Minified using Disk

Served from: www.anujvarma.com @ 2026-06-07 10:08:09 by W3 Total Cache
-->