C#:存储百分比,50或0.50? [英] C#: Storing percentages, 50 or 0.50?

查看:417
本文介绍了C#:存储百分比,50或0.50?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当变量控股百分比值是有拿着他们作为整数VS分数之间的偏好。即应变量持有0和100之间,或在0.00和1.00之间的数字?在这两种情况下,该变量保持的值是小数类型的

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.

我与交互的数据库恰好它们存储为整数0到100

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

请注意:我使用的是整数这个词来表示值〜100 0虽然这些数值可能包含小数部分(例如,25.75)。我不知道该怎么形容百分比值的两个区域之间的差异

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

推荐答案

我会倾向于存储它们如0到1,那么没有使用在计算中的数目时,需要丁文

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

使用0和100之间的数字是更多的显示/ readbility方式看的数目。当你显示比例可以使用的String.Format({0:P},百分比)
将预计数为0和1之间但将显示为0至100。

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.

这篇关于C#:存储百分比,50或0.50?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆