哪种类型可以节省百分比(Which type to save percentages)

编程入门 行业动态 更新时间:2024-10-17 17:15:28
哪种类型可以节省百分比(Which type to save percentages)

使用double类型来存储百分比值是合适的吗(例如,商店应用程序中的折扣百分比)还是使用decimal类型会更好?

Is it appropriate to use the double type to store percentage values (for example a discount percentage in a shop application) or would it be better to use the decimal type?

最满意答案

浮点类型( float和double特别适合财务应用程序。

财务计算几乎总是十进制的,而浮点类型几乎总是二元的。 很容易用十进制表示的许多常见值都不可能用二进制表示。 例如, 0.2d = 0.00110011...b 。 请参阅http://en.wikipedia.org/wiki/Binary_numeral_system#Fractions_in_binary进行良好的讨论。

同样值得讨论的是如何在系统中表示价格。 decimal是一个不错的选择,但浮点不是,因为上面列出的原因。 因为你相信面向对象编程,所以你会用新的Money类型来包装这个decimal ,对吧? 对Kent Beck的Test Driven Development by Example进行了很好的金钱处理。

也许你会考虑将百分比表示为整数,然后每次使用时除以100。 然而,你正在设置自己的错误(哎呀,我忘了分割)和未来的不灵活性(客户需要1/10的百分比,所以去修复每/100 /1000糟糕,错过了一个bug)。

这给你留下了两个很好的选择,这取决于你的需求。 一个是decimal 。 对于10%这样的整体百分比来说非常好,但是对于像“今天只有三分之一!”这样的事情来说,这不是很好,因为1/3不能完全代表十进制。 如果以1/3的比例购买3个东西作为整数出现,你会喜欢它,对吧?

另一种是使用Fraction类型,它存储一个integer分子和分母。 这使您可以表示所有有理数的精确值。 要么实现你自己的Fraction类型,要么从库中选择一个(搜索互联网)。

Floating-point types (float and double are particularly ill-suited to financial applications.

Financial calculations are almost always decimal, while floating-point types are almost always binary. Many common values that are easy to represent in decimal are impossible to represent in binary. For example, 0.2d = 0.00110011...b. See http://en.wikipedia.org/wiki/Binary_numeral_system#Fractions_in_binary for a good discussion.

It's also worth talking about how you're representing prices in your system. decimal is a good choice, but floating point is not, for reasons listed above. Because you believe in Object Oriented Programming, you're going to wrap that decimal in a new Money type, right? A nice treatment of money comes in Kent Beck's Test Driven Development by Example.

Perhaps you will consider representing percentages as an integer, and then dividing by 100 every time you use it. However, you are setting yourself up for bugs (oops, I forgot to divide) and future inflexibility (customer wants 1/10ths of a percent, so go fix every /100 to be /1000. Oops, missed one - bug.)

That leaves you with two good options, depending on your needs. One is decimal. It's great for whole percentages like 10%, but not for things like "1/3rd off today only!", as 1/3 doesn't represent exactly in decimal. You'd like it if buying 3 of something at 1/3rd off comes out as a whole number, right?

Another is to use a Fraction type, which stores an integer numerator and denominator. This allows you to represent exact values for all rational numbers. Either implement your own Fraction type or pick one up from a library (search the internet).

更多推荐

use,百分比,decimal,double,类型,电脑培训,计算机培训,IT培训"/> <meta name="des

本文发布于:2023-08-05 21:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百分比   节省   哪种类型   percentages   save

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!