Brian's Blog

items I see across my tribes

C# data type Refresher with Financial Issue

February 07
by briancarter 7. February 2010 11:03

We all need a refresher on the basics.  I’ve covered data types before, but the hierarchy below sums it up in a clear diagram:

The C# type hierarchy.

 

At the leafs, the float vs Integral is clearly shown.  Integrals (Integer) are viewed as subset of the real numbers, they are numbers that can be written without a fractional or decimal component. For example, 65, 7, and −756 are integers; 1.6 and 1½ are not integers. The set of all integers is often denoted by a boldface Z.

The C# standard only lists double and float as floating points available (those being the C# shorthand for System.Double and System.Single), but strictly speaking the decimal type (shorthand for System.Decimal) is also a floating point type.

The decimal type is just another form of floating point number - but unlike float and double, the base used is 10.  Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

image

 

Financial Issue Testing

Let’s review financial situations. Say we are using floats to track the money. 

123,456.78 + 123.45 = $123,580.2  --> should calculate to $123,580.23

What happen – float only can support 7 digits, so C# converted number to 123456.8 + 123.45 – since last digit is 5 or less – it rounded down.  If the number would of ended in a 6 or above – it would of rounded up.  Lost $0.03 – do this to many times in your shopping cart and it will add up.

Do you have financial reports for month/year end? 

float x1 = float.Parse("1234567890.12");  //1.234568E+9
x1.ToString();  //"1.234568E+09"

oops… off by over $110 bucks.  Use decimal type for financial fields.

 

Stress testing float – double - decimal

--------- Show limits of float  ----------------

123.4567
float = 123.4567, double = 123.4567, decimal = 123.4567

123.45678
float = 123.4568, double = 123.45678, decimal = 123.45678

--------- Show limits of double  ----------------

123.456789012345
float = 123.4568, double = 123.456789012345, decimal = 123.456789012345

123.4567890123456
float = 123.4568, double = 123.456789012346,
decimal = 123.4567890123456

--------- Show limits of decimal  ----------------

123.45678901234567890123456789
float = 123.4568, double = 123.456789012346, decimal = 123.45678901234567890123456789

123.456789012345678901234567899
float = 123.4568, double = 123.456789012346, decimal = 123.45678901234567890123456790

Categories: Development

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.