Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Decimal Arithmetic 1

Status
Not open for further replies.

Rewbs

Programmer
May 1, 2002
18
GB
Hi all,
2 points:

1. I'm looking for a calculation that would show the advantages of using the decimal primitive in C#, ie a calculation that would yield different results when using floats and decimals.

I Java such calculations would be repeatedly dividing 9 by 10, or 0.70 * 1.05. With C#, these give correct results even with floats and doubles.


2. What is the point of the Decimal class? does it offer any advantages over the decimal primitive, or is it just a wrapper class?

Cheers,
Robin
 
The decimal class is most useful when dealing with money, as you don't want fractions of a cent being generated/dropped when calculating interest.

Chip H.
 
Thanks for your reply. Could you possibly elaborate on this? What exactly does the Decimal class do that the decimal primitive doesn't?
 
I'm sorry, I misunderstood your question (most people ask why use the decimal datatype over the float or double datatype).

Using the Decimal class vs. the decimal C# datatype is really a matter of taste. In the C# programmer's reference, there's a table called Built-in Types, which shows that the decimal datatype is an alias for System.Decimal, so they're equivalent.

If you think your code will ever be ported to another .NET language (VB, COBOL, etc) then use the one from the framework, as that will require less work during your port.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top