When you need to deal with a number that is a fraction, what do you specify for its type? If you are like most programmers I know, you’ll reach for Float (Single if you are using VB) or Double.
If you are working with currency, though, this could get you into a lot of trouble.
When you store a number into a float, you are not storing an exact number. This is because the number you are storing is an approximation of the number you entered. When you store a number, the integer portion of the number gets priority and the fractional part gets entered as close as is possible given the size of the type you are storing it as.
That is, a Double will be able to save the information more accurately than the Float, but neither of them will store the information precisely.
And that’s just storing a number we enter directly. What happens if we need to multiply or divide that number?
For simplicity, let’s just say we need to divide a dollar by three. How would that get stored accurately into a float or a double? The answer is that it won’t. It will store as many threes on the right side of the decimal as possible.
This is what the Decimal data type was created for. A decimal data type deals with the data more like we would if we were dealing with the problem with paper and pencil. In effect, a decimal data type is similar to an integer that has a decimal point.
Let me illustrate. Let’s imagine that we have an integer that holds our value and another that holds how many decimal places the value has.
int mainValue; int decmialPlaces;
mainValue = 100; decimalPlaces = 2;
Now, we want to divide our dollar by three:
mainValue = mainValue / 3;
If we multiplied the result by 3, we’d get .99, which is what we’d expect. We still have to account for that lost penny, but we would deal with the lost penny using standard accounting practices.
Fortunately, we don’t have to go to all that trouble because this is exactly why the decimal type was created for us.
decimal money = 1.00M; money = money / 3;
I once went to consult in a project that was entirely currency based. It was basically an accounting package. This application was not only dealing with money, but it also had to deal with conversion rates between countries. They wanted to know why they were losing pennies when they converted from one currency to another when the entire application was using floats (not even doubles) instead of decimal values. That handled most of the problem for them. The second recommendation I had was to choose a base currency type and always convert from that currency to all of the other countries rather than converting from country A to country B and then back to country A.
Other post in c#
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Simple Properties in C# 3.5 - November 22nd, 2007
- CSharp adds the var keyword! - November 27th, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- Computed Columns Using LINQ to SQL Classes - February 12th, 2008
- DataSets, ObjectDataSource and 3-Tier ASP.NET - February 29th, 2008
- Serializable attribute vs Serializable Interface - April 9th, 2008
- Why you can't cast an integer to a string. - June 23rd, 2008
- Unsafe Mode in C# - December 15th, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- Upload a File via WebRequest Using CSharp - November 10th, 2009
- Azure CDN Blob Storage Case Sensitive Issue - April 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Decimal vs Float (Single) or Double - September 18th, 2012
- C# Self Executing Anonymous Function - February 28th, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
- CSharp Enum - October 2nd, 2013
- CSharp Numeric Overflows - July 3rd, 2014
- CSharp IDisposable Confusion - July 17th, 2014
- Why Get Certified? - July 24th, 2014
- C# Properties Get and Set - November 13th, 2014
- Value Type vs Reference Type - November 27th, 2014
- String and StringBuilder - December 11th, 2014
- Browser Automation in .NET w/ Chromium - July 9th, 2015
- CefSharp Offscreen [Why do I have so many instances of Chromium?] - October 1st, 2015