C#: Storing percentages, 50 or 0.50?

后端 未结 5 1027
情书的邮戳
情书的邮戳 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条回答
  •  萌比男神i
    2021-02-19 11:22

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

    Using a number between 0 and 100 is more of a display / readability 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.

提交回复
热议问题