Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 640 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the best data type to use for money in C#?

#1
What is the best data type to use for money in C#?
Reply

#2
Decimal. If you choose double you're leaving yourself open to rounding errors
Reply

#3
decimal has a smaller range, but greater precision - so you don't lose all those pennies over time!

Full details here:

[To see links please register here]

Reply

#4
Create your own class. This seems odd, but a .Net type is inadequate to cover different currencies.
Reply

#5
Agree with the Money pattern: Handling currencies is just too cumbersome when you use decimals.

If you create a Currency-class, you can then put all the logic relating to money there, including a correct ToString()-method, more control of parsing values and better control of divisions.

Also, with a Currency class, there is no chance of unintentionally mixing money up with other data.
Reply

#6
Another option (especially if you're rolling you own class) is to use an int or a int64, and designate the lower four digits (or possibly even 2) as "right of the decimal point". So "on the edges" you'll need some "* 10000" on the way in and some "/ 10000" on the way out. This is the storage mechanism used by Microsoft's SQL Server, see

[To see links please register here]


The nicity of this is that all your summation can be done using (fast) integer arithmetic.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through