简体   繁体   English

在Delphi中,如何确定何时使用Real,Real48,Double或Single数据类型?

[英]In Delphi how do I determine when to use Real, Real48, Double or Single data types?

Most of my applications revolve around financial calculations involving payments and interest rate calculations. 我的大多数申请都围绕着涉及支付和利率计算的财务计算。 I'm looking to find out how to determine what Delphi data type is best to use. 我正在寻找如何确定最适合使用的Delphi数据类型。

If I'm using a database to store these values and I've defined the fields in that database to be a decimal value with two decimal places, which Delphi datatype is most compatible with that scenario? 如果我使用数据库来存储这些值,并且我已将该数据库中的字段定义为带有两个小数位的十进制值,哪种Delphi数据类型与该方案最兼容?

Should I use a rounding formula in Delphi to format the results to two decimal places before storing the values in the database? 在将值存储在数据库中之前,我是否应该使用Delphi中的舍入公式将结果格式化为两位小数? If so what is a best practice for doing so? 如果是这样,这样做的最佳做法是什么?

If you want to do financial calculations, don't use any of the floating-point/real types. 如果要进行财务计算,请不要使用任何浮点/实数类型。 Delphi has a Currency type, which is a fixed-point value with 4 decimal places, that should be just what you need. Delphi有一个Currency类型,它是一个带有4位小数的定点值,应该就是你需要的。

For such calculations, don't use floating point types like Real, Single or Double. 对于此类计算,请勿使用Real,Single或Double等浮点类型。 They are not good with decimal values like 0.01 or 1234.995, as they must approximate them. 它们不适合像0.01或1234.995这样的小数值,因为它们必须近似它们。

You can use Currency, a fixed point type, but that is still limited to 4 decimal places. 您可以使用货币,固定点类型,但仍然限制为4位小数。

Try my Decimal type, which has 28-29 places and has a decimal exponent so it is ideal for such calculations. 尝试我的Decimal类型,它有28-29个位置并且有一个十进制指数,因此它非常适合这种计算。 The only disadvantage is that it is not FPU supported (but written in assembler, nevertheless) so it is not as fast as the built-in types. 唯一的缺点是它不支持FPU(但是用汇编语言编写),因此它没有内置类型那么快。 It is the same as the Decimal type used in .NET (but a little faster) and quite similar to the one used on the Mac. 它与.NET中使用的Decimal类型相同(但速度稍快),与Mac上使用的类似。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM