views:

142

answers:

5

When holding percentage values in variables is there a preference between holding them as whole numbers vs fractions. That is should the variable hold numbers between 0 and 100 or between 0.00 and 1.00? In either case, the variable holding the values is of decimal type.

The database I'm interacting with happens to store them as whole numbers 0 to 100.

Note: I'm using the word "whole number" to denote values in the range 0 to 100 though those values may contain fractional components (e.g., 25.75). I don't know how else to describe the difference between the two ranges of percentage values

+3  A: 

If you are using floating point fields (float, decimal, double), then 50.0 and 0.50 will have the same degree of precision. So, from this point, I would make decisions based on what similar fields in similar tables do, to give a sense of design unity.

maxwellb
Uhm... in that exact case, yes, but what about `10.0` vs `0.10`? Not the same at all!
Mark
... reaaaaally?
maxwellb
@Mark: What are you trying to point out? Different storage forms will mandate different ways of using the numbers.
Karmastan
I agree with the design unity statement. Hardware repesentation of floating-point numbers .50 vs 50.00 could give (significantly?) different results in math operations, even accounting for the order-of-magnitude differences.
DaveE
Huh? I'm trying to point out that `.5` can be exactly represented in binary, but `.1` cannot be. Thus, storing the numbers in the range 0-100 vs 0-1 is *not* the same at all, because the later case could cause significant rounding errors...
Mark
.1 and .5 as floating point values have the same degree of precision
maxwellb
+7  A: 

I would be inclined to store them as 0 to 1, then there is no coversion required when using the number in a calculation.

Using a number between 0 and 100 is more of a display / readbility way of looking at the number. When you are displaying the percentage you can use String.Format("{0:P},percentage) which will expect the number to be between 0 and 1 but will display as 0 to 100.

benPearce
2nded. It's easier to work with percentages in the range 0-1.. and you normally only multiply it by one other number if you want to draw a progress bar or something like that, so there really won't be any significant rounding errors.
Mark
A: 

If you are doing a lot of math with fractions, you want to store the numerator and denominator separately as whole numbers. When using fractions to represent repeating or non-repeating decimals you can get rounding errors that grow the more operations you have.

If you are just storing and presenting the number, then it really doesn't matter.

What math do you plan to do with this number, if any?

MatthewMartin
A: 

You can use doubles to represent both percentages and 0-1. But if you choose to store percentage as "whole number" I would advise against using integers, which would limit you if you ever want to represent 12.3%, for example.

Assaf Lavie
A: 

I think this is trivial... both are the same

I mean in terms of calculations/displaying info...

00.50 is cool for calculations

50.00 is cool for displaying
Luiscencio
They are not the same, by a factor of 100!
benPearce