Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the best data type to use for money in c#?

What is the best data type to use for BIT in c#?
Posted
Updated 26-Sep-19 17:18pm
v3

 
Share this answer
 
Comments
[no name] 27-Oct-11 11:40am    
+5, seems to be a tie though :-)
It even said the same amount of seconds when it was under a minute :-P
Nish Nishant 27-Oct-11 11:43am    
Sorry :-)
net_prog 27-Oct-11 12:56pm    
Always decimal for money.
Decimal[^] IMO, however I often have it wrapped in another object. That way you can store currency type and other features you might need.

[EDIT - Seems you added after this repsonce]
Small money is not relavant in C#, just as SmallDate is not. If you are storing it as such in the DB you will need to check you values as the user enters them or before you send to the DB.
All the more reason you should wrap it in an object though. You could add some property that keeps track of the back storage type

C#
public enum MoneyType
{
   Small,
   Large
}


Set the property on building of the money object. Then in the Value property setter you could check if it meets the constraints of small (that is if it is small), and handle how you want if it is not (i.e. error message, truncate, ignore etc.)
 
Share this answer
 
v6
Comments
Nish Nishant 27-Oct-11 11:43am    
My 5. Nice to have people back your answer up *grin*
[no name] 27-Oct-11 11:51am    
Exactly! *Bobbles Head* :P
decimal[^]

hope it helps :)
 
Share this answer
 
Comments
Nish Nishant 27-Oct-11 11:43am    
Dude, 3rd guy to post the same thing :-)
Uday P.Singh 27-Oct-11 11:47am    
just 6 secs late LOL :)
Decimal. It's the only floating point type that doesn't suffer from approximation errors, but it's slower to use and has a smaller range.
 
Share this answer
 
Comments
Nish Nishant 27-Oct-11 11:44am    
And no. 4! :-)
[no name] 27-Oct-11 11:52am    
Well atleast the OP will have confidence in his choice :-P
 
Share this answer
 
Comments
fjdiewornncalwe 27-Oct-11 12:20pm    
Correct, but you only get a 3 because the same answer has been given 4 times already before you did. Please don't post duplicates of existing answers. Ones within the same minute are acceptable because the other answers wouldn't have existed yet from the perspective of those users.
I know the opinion here seems to rest resolutely on decimal, but that can be overkill. These things are 16 bytes wide which gives them extreme precision but that will come at a performance and storage cost. Your database types are only 4 and 8 bytes wide.

If you need that precision then decimal is your choice, but if you don't I'd consider using a smaller type.
 
Share this answer
 
Comments
[no name] 27-Oct-11 13:36pm    
As much as your remarks are accurate and useful you should provide alternatives to the OP. Otherwise you aren't really providing a solution, just providing an anti-solution.
Rob Philpott 27-Oct-11 18:42pm    
Sorrry, not clear I understand your point. Please clarify.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900