如何重新present货币或资金使用C [英] How to represent currency or money in C

查看:124
本文介绍了如何重新present货币或资金使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR 1 什么是在C再presenting货币或货币准确和维护的方法。

TL;DR1 What is an accurate and maintainable approach for representing currency or money in C?

背景的问题:结果
这已经回答了许多其他语言,但我无法找到C语言了坚实的答案。

Background to the question:
This has been answered for a number of other languages, but I could not find a solid answer for the C language.

的Java 为什么不使用双人或浮动重新present货币?

的Objective-C 如何重新在目标C / iPhone present钱

<子>的注:有很多其他语言的更多类似的问题,我刚拔掉重新presentational目的几个

所有这些问题都可以蒸馏下来到特定类型可能会有所不同取决于语言用小数数据类型。

All of those questions can be distilled down to "use a decimal data type" where the specific type may vary based upon the language.

有是关系问题上 的最终使用定点的做法暗示,但没有答案的解决使用C中的特定数据类型。

There is a related question that ends up suggesting using a "fixed point" approach, but none of the answers address using a specific data type in C.

同样,我已经看过任意precision库如 GMP ,但它不是我清楚,如果这是使用或不是最好的方法。

Likewise, I have looked at arbitrary precision libraries such as GMP, but it's not clear to me if this is the best approach to use or not.

简化的假设:

Simplifying Assumptions:


  • presume的x86或x64架构,但请拨打出会影响基于RISC架构,如电源芯片或者ARM芯片做任何假设。

  • Presume an x86 or x64 based architecture, but please call out any assumptions that would impact a RISC based architecture such as a Power chip or an Arm chip.

在计算精度是首要的要求。维护的易于将是下一个要求。计算速度是很重要的,但第三的其他要求。

Accuracy in calculations is the primary requirement. Ease of maintenance would be the next requirement. Speed of calculations is important, but is tertiary to the other requirements.

计算需要能够安全地支持操作精确到以及配套的数值范围达万亿(10 ^ 9)

Calculations need to be able to safely support operations accurate to the mill as well as supporting values ranging up to the trillions (10^9)

其他问题的区别:

Differences from other questions:

如上所述,这种类型的问题的之前已经为多个其他语言问。这个问题是从一对夫妇的原因,其他问题不同。

As noted above, this type of question has been asked before for multiple other languages. This question is different from the other questions for a couple of reasons.

使用公认的答案:为什么不使用双人或浮动重新present货币,让我们的亮点的差异。

Using the accepted answer from: Why not use Double or Float to represent currency?, let's highlight the differences.

解决方案1 ​​),在几乎任何语言的使用,而不是整数,并计算美分的解决方案。举例来说,1025将是$ 10.25。几种语言也内置类型来处理资金。 (解决方案2 )其中,Java有BigDecimal类和C#有小数类型。

(Solution 1) A solution that works in just about any language is to use integers instead, and count cents. For instance, 1025 would be $10.25. Several languages also have built-in types to deal with money. (Solution 2) Among others, Java has the BigDecimal class, and C# has the decimal type.

<子>的着重强调要突出两个建议的解决方案

的第一溶液基本上是固定点的方法的一个变种。存在与所建议的范围(跟踪美分)该溶液中的问题是没有足够用于基于轧机计算和显著信息将丢失上舍入。

The first solution is essentially a variant of the "fixed point" approach. There is a problem with this solution in that the suggested range (tracking cents) is insufficient for mill based calculations and significant information will be lost on rounding.

另一个解决方案是使用原生小数类,这是不是在C语言中。

The other solution is to use a native decimal class which is not available within C.

同样,回答不考虑其他选项,如创建用于处理这些计算或使用任意precision库结构。这些都是可以理解的差异Java没有结构和为什么要考虑一个第三方库的时候有在语言中的原生支持。

Likewise, the answer doesn't consider other options such as creating a struct for handling these calculations or using an arbitrary precision library. Those are understandable differences as Java doesn't have structs and why consider a 3rd party library when there is native support within the language.

这个问题是从这个问题和其他相关问题有所不同,因为C没有原生类型支持同一水平,并具有语言功能的其他语言则没有。而我还没有看到任何其他的问题解决,这可能在C走近多种方式。

This question is different from that question and other related questions because C doesn't have the same level of native type support and has language features that the other languages don't. And I haven't seen any of the other questions address the multiple ways that this could be approached in C.

问题:结果
从我的研究,似乎浮动不是一个合适的数据类型来使用一个C程序中重新present货币由于浮点错误。

The Question:
From my research, it seems that float is not an appropriate data type to use to represent currency within a C program due to floating point error.

我应该用在C重新present钱,这是为什么做法比其他方法更好呢?

What should I use to represent money in C, and why is that approach better than other approaches?

1 <子>的这个问题在较短的形式启动,但得到的反馈表明,需要澄清的问题。

推荐答案

要么使用一个整数数据类型(很长很长,长,INT)或BCD(二进制codeD十进制)运算库。你应该存储的最小的金额,就会显示十分之一或百分之一。也就是说,如果您使用的是美元和presenting美分(一美元的百分之一),你的数值应为整数,再presenting钢厂或millrays(十分之一或百分之一的百分之一)。额外显著数字将保障您的利益和相似的计算圆一致。

Either use an integer data type (long long, long, int) or a BCD (binary coded decimal) arithmetic library. You should store tenths or hundredths of the smallest amount you will display. That is, if you are using US dollars and presenting cents (hundredths of a dollar), your numeric values should be integers representing mills or millrays (tenths or hundredths of a cent). The extra significant figures will ensure your interest and similar calculations round consistently.

如果您使用的是整数类型,确保它的范围足够大,以处理关注的金额。

If you use an integer type, make sure that its range is great enough to handle the amounts of concern.

这篇关于如何重新present货币或资金使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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