Click here to Skip to main content
11,496,146 members (820 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
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.
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

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.)
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

hope it helps Smile | :)
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 :)
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.
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
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.
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.
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 Dnyaneshwar@Pune 692
1 RyanDev 230
2 CHill60 163
3 Sascha Lefèvre 145
4 Sergey Alexandrovich Kryukov 124
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 28 Oct 2011
Copyright © CodeProject, 1999-2015
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