C#: Storing percentages, 50 or 0.50?

后端 未结 5 1021
情书的邮戳
情书的邮戳 2021-02-19 10:53

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

5条回答
  •  情深已故
    2021-02-19 11:31

    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?

提交回复
热议问题