简体   繁体   English

我应该使用哪些类型来表示C#和SQL Server中的百分比?

[英]What types should I use to represent percentages in C# and SQL Server?

I'm thinking floats. 我在想漂浮物。 For the record I'm also using NHibernate. 为了记录,我也使用NHibernate。

decimal

由于四舍五入,你不会失去精确度。

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 (eg a set of calculated weights that add up to 100% is probably better represented as a float) 但有时最好使用浮点数来提高精度(例如,一组计算的权重加起来可以更好地表示为浮点数)

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.. ie 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). 采取预防措施,以确保你在整个领域的使用是一致的...即不要将它存储为浮点数(n = .5),然后尝试重新组合它,就好像它是代码的另一部分中的整数(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 float s your boat. 只要你确保不这样做并且你没有制造需要30位精确数字的激光,只需在int,double或任何漂浮的船之间选择你最喜欢的味道。 waka-waka. 瓦卡,瓦卡。

It depends on how accurate you need. 这取决于你需要多准确。 If you don't need any decimal places you could even use tiny int. 如果你不需要任何小数位,你甚至可以使用tiny int。 But generally floats are good if you need some decimal places. 但是如果你需要一些小数位,通常浮点数很好。 I use LLBLGen and floats for %'s 我使用LLBLGen并浮动%

With regard to SQL Server, it's rare that I'd store a percentage. 关于SQL Server,我很少存储一个百分比。 9 times out of 10 you want to store the data that is used to derive the percentage and calculate as needed. 您希望存储用于获取百分比的数据并根据需要进行计算的9次中的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. 使用之前建议的小数来避免舍入问题。

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. 如果是显示,int就可以了,并且比浮点快。 If it's for infrequent mathematics, an int would still do fine (or even a short, in either case). 如果它是不经常的数学,那么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. 编辑:当然,'int'假设您只使用严格的整数百分比。 If you aren't, you'd ALWAYS be better with float or dec. 如果你不是,你总是更好的浮动或dec。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用什么数据类型来表示 C# 中的钱? - What data type should I use to represent money in C#? 我应该在SQL Server还是C#中使用“用户定义函数”? - Should I use “User Defined Functions” in SQL server, or C#? 代替C#Windows应用程序中的dataGridView,我应该如何使用控制台应用程序显示来自sql server的表? - Instead of dataGridView in C# windows application, what should I use for console application to display a table from sql server? C#double可以表示SQL Server float不能表示哪些特定值? - What specific values can a C# double represent that a SQL Server float can't? 我应该使用Enum,静态类,字典或结构来表示C#中的这些“标记浮点数”吗? - Should I use Enum, static Class, Dictionary or Struct to represent these “labeled floats” in C#? C#/ SQL Server 2005我应该对报表和数据网格使用OOP吗? - C# / SQL Server 2005 Should I use OOP for reports and datagrids? 我应该使用什么C#模板为XNA游戏构建服务器? - What C# template should I use to build a server for an XNA game? 在我的场景中,我应该在WPF和c#中使用哪种类型的线程……常规线程,后台工作程序或线程池? - What type/types of threading should I use in WPF and c# for my scenario… regular threads, background worker, or thread pool? 我应该在此用例中使用哪些C#集合? - What C# collection should I use for this use case? 我应该使用什么 .NET class 来表示 su.net 及其掩码? - What .NET class should I use to represent a subnet and its mask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM