tags:

views:

640

answers:

7

I'm thinking floats. For the record I'm also using NHibernate.

+5  A: 

decimal

You won't lose precision due to rounding.

Bob King
Coming across this question later, I found this answer more helpful WRT the SQL Server portion of the question: http://stackoverflow.com/questions/158966/what-types-should-i-use-to-represent-percentages-in-c-and-sql-server/159780#159780. It would seem the answer is decimal as the CLR type and float for SQL Server type, at least if you are likely to store fractional percentages.
I Have the Hat
+1  A: 

it depends on how accurate you need. If you dont need any decimal places you could even use tiny int. But generally floats are good if you need some decimal places. I use LLBLGen and floats for %'s

mattlant
A: 

It depends on what you are using them for.

if it's for display, an int would do just fine, and be faster than a float. If it's for infrequent mathematics, an int would still do fine (or even a short, in either case).

If you're doing a lot of math, then a float would probably be best, performance-wise.

Of course, unless you're doing a LOT of manipulation of the percentages, it won't really matter in the end, performance-wise, given modern processor speed.

EDIT: Of course, 'int' assumes you are just using strict, whole-number percents. If you aren't, you'd ALWAYS be better with float or dec.

Jeff
A: 

With regard to SQL Server, it's rare that I'd store a percentage. 9 times out of 10 you want to store the data that is used to derive the percentage and calculate as needed.

If and only if you have empirical data showing the calculation is too slow, then go ahead store it. Use a decimal as previously suggested to avoid rounding issues.

Joel Coehoorn
this could be domain specific. There are often times you wont have the data that calcs the percent, such as a user entering the % of a colour to use
mattlant
As @mattlant said, the percentages I need to store are domain percentages such as 17.5% VAT and 40% discount etc.
IainMH
A: 

It largely depends on how much precision you need; The most important thing is to be consistent and clear. Take precautions to ensure that you are consistent across the field's use.. i.e. don't store it as a float (n = .5) and then try to reconstitute it as if it were an integer in another part of your code (mypercentage = n/100). As long as you make sure not to do that and you are not building a laser that requires 30 significant digits of precision, just pick your favorite flavor between int, double, or whatever floats your boat. waka-waka.

BKimmel
+2  A: 

The answer is application-dependent.

Others have pointed out that decimal is better than float for representing an exact value. But sometimes it's better to use a float for the increased precision (e.g. a set of calculated weights that add up to 100% is probably better represented as a float)

Joe
+2  A: 

Floats. Decimal loses precision due to rounding as proved fairly conclusively by Jeff here: http://www.sqlservercentral.com/Forums/Topic522397-360-1.aspx

Use floats, and output to a set number of decimal places as required.

Meff