Last post Oct 12, 2016 03:05 PM by Rion Williams
Oct 12, 2016 02:19 PM|JAYHAWKER|LINK
I am curios as to what is a best practices method of storing percentages in a database. If it is 3%, I tend to store the 3. But I am just curios what others do considering you always have to divide by 100 to do math, and converting to percentage with .ToString("P2")
multiplies it by 100.
Oct 12, 2016 02:40 PM|0belix|LINK
I realy don't know if there is a best practice for this, but i usualy don't store percentages on the database, but rather the numbers that lead to them, and do the math via the store procedure or the codebehind that calls it. But if you really want to store
the actual percentage, i'd opt for either store it as an integer (25% = 25) or if you need to use it to do some math inside the DB, store it as a decimal (25% = 0.25)
Oct 12, 2016 03:05 PM|Rion Williams|LINK
I agree with Obelix here.
Percentages or at least values with percent signs generally fall under "formatting" in my option and as a result I generally avoid storing them that way (the same way you should store a timestamp instead of an actual formatted date string).
Depending on your level of precision, a decimal or float value might be best (i.e. 0.25, 0.125, 1.75, etc.) as that's a better mathematical representation of a percentage (generally a value between 0 (0%) and 1 (100%). If you would never need any fractional
percentages, then I suppose an integer could be valid as well.