我应该使用什么类型来表示 C# 和 SQL Server 中的百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/158966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What types should I use to represent percentages in C# and SQL Server?
提问by Iain Holder
I'm thinking floats. For the record I'm also using NHibernate.
我在想漂浮物。为了记录,我也在使用 NHibernate。
采纳答案by Bob King
decimal
decimal
You won't lose precision due to rounding.
你不会因为四舍五入而失去精度。
回答by mattlant
It depends on how accurate you need. If you don't need any decimal places you could even use tiny int. But generally floats are good if you need some decimal places. I use LLBLGen and floats for %'s
这取决于您需要多准确。如果您不需要任何小数位,您甚至可以使用 tiny int。但是如果您需要一些小数位,通常浮点数是好的。我使用 LLBLGen 并浮动 %'s
回答by Jeff
It depends on what you are using them for.
这取决于您使用它们的目的。
if it's for display, an int would do just fine, and be faster than a float. If it's for infrequent mathematics, an int would still do fine (or even a short, in either case).
如果用于显示,则 int 会很好,并且比浮点数快。如果是不常用的数学, int 仍然可以(或者甚至是短的,在任何一种情况下)。
If you're doing a lot of math, then a float would probably be best, performance-wise.
如果您要进行大量数学运算,那么在性能方面,浮点数可能是最好的。
Of course, unless you're doing a LOT of manipulation of the percentages, it won't really matter in the end, performance-wise, given modern processor speed.
当然,除非您对百分比进行大量操作,否则考虑到现代处理器的速度,最终在性能方面并不重要。
EDIT: Of course, 'int' assumes you are just using strict, whole-number percents. If you aren't, you'd ALWAYS be better with float or dec.
编辑:当然,'int' 假设您只是使用严格的整数百分比。如果不是,则使用 float 或 dec 总是更好。
回答by Joel Coehoorn
With regard to SQL Server, it's rare that I'd store a percentage. 9 times out of 10 you want to store the data that is used to derive the percentage and calculate as needed.
关于 SQL Server,我很少会存储一个百分比。10 次中有 9 次您想要存储用于导出百分比和根据需要计算的数据。
If and only if you have empirical data showing the calculation is too slow, then go ahead store it. Use a decimal as previously suggested to avoid rounding issues.
如果且仅当您有经验数据显示计算速度太慢时,请继续存储它。使用之前建议的小数以避免四舍五入问题。
回答by BKimmel
It largely depends on how much precision you need; The most important thing is to be consistent and clear. Take precautions to ensure that you are consistent across the field's use.. i.e. don't store it as a float (n = .5) and then try to reconstitute it as if it were an integer in another part of your code (mypercentage = n/100). As long as you make sure not to do that and you are not building a laser that requires 30 significant digits of precision, just pick your favorite flavor between int, double, or whatever floats your boat. waka-waka.
这在很大程度上取决于您需要多少精度;最重要的是要保持一致和清晰。采取预防措施以确保您在整个字段的使用中保持一致.. 即不要将其存储为浮点数 (n = .5),然后尝试将其重构为代码的另一部分中的整数 (mypercentage = n/100)。只要您确保不这样做并且您没有构建需要 30 位有效数字精度的激光器,只需在 int、double 或任何您的船的float之间选择您最喜欢的口味。瓦卡瓦卡。
回答by Joe
The answer is application-dependent.
答案取决于应用程序。
Others have pointed out that decimal is better than float for representing an exact value. But sometimes it's better to use a float for the increased precision (e.g. a set of calculated weights that add up to 100% is probably better represented as a float)
其他人指出,在表示精确值方面,十进制比浮点更好。但有时最好使用浮点数来提高精度(例如,一组加起来为 100% 的计算权重可能更好地表示为浮点数)