Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What types should I use to represent percentages in C# and SQL Server?

Tags:

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

like image 547
Iain Holder Avatar asked Oct 01 '08 18:10

Iain Holder


People also ask

What data type should be used for percentage?

For (0.00% to 100.00%) you need decimal(5,2) and for (0% to 100%) it's better to use int if you don't need decimal points.

What data type is percentage in C#?

CSharp Online Training The percent ("P") format specifier is used to multiply a number by 100. It converts the number into a string representing a % (percentage). In the same way, using (“P1”), would only include a single vale after decimal-point.

Is a percentage An integer or float?

The most common way to represent a percentage is with floating-point numbers, that way you have some decimal precision such as 33.25 percent. The thing to always remember is what percent really means. It means Per 100.


2 Answers

decimal

You won't lose precision due to rounding.

like image 165
Bob King Avatar answered Oct 05 '22 11:10

Bob King


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)

like image 32
Joe Avatar answered Oct 05 '22 11:10

Joe