Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 27-Oct-11 6:35am
Edited 28-Oct-11 1:42am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Collin Jasnoch at 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
Nishant Sivakumar at 27-Oct-11 11:43am
   
Sorry :-)
net_prog at 27-Oct-11 12:56pm
   
Always decimal for money.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
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.)
  Permalink  
v6
Comments
Nishant Sivakumar at 27-Oct-11 11:43am
   
My 5. Nice to have people back your answer up *grin*
Collin Jasnoch at 27-Oct-11 11:51am
   
Exactly! *Bobbles Head* :P
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

decimal[^]
 
hope it helps Smile | :)
  Permalink  
Comments
Nishant Sivakumar at 27-Oct-11 11:43am
   
Dude, 3rd guy to post the same thing :-)
Uday P.Singh at 27-Oct-11 11:47am
   
just 6 secs late LOL :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
Nishant Sivakumar at 27-Oct-11 11:44am
   
And no. 4! :-)
Collin Jasnoch at 27-Oct-11 11:52am
   
Well atleast the OP will have confidence in his choice :-P
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Comments
Marcus Kramer at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  
Comments
Collin Jasnoch at 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 at 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)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 BillWoodruff 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 28 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100