C 十进制数据类型

新手上路,请多包涵

有没有办法在我的 C++ 程序中使用十进制数据类型,例如 decimal32decimal64decimal128

原文由 fpiro07 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 817
2 个回答

并非所有编译器都实现了 Decimal TR 中的类。一些编译器,例如 gcc ,实现了 C Decimal TR 并在 C++ 中提供了相应的扩展。过去有一个可用的 C++ Decimal TR 的开源实现,但我找不到它。如果您的编译器不支持小数类型,那么您最好的选择可能是为 IBM 的 decNumber library 创建一个包装器。

为了改善 C++ 未来的情况,我 制定了更新 TR 的计划, 我将把当前的 TR 变成一个完整的提案,为下一次 C++ 委员会会议(4 月在布里斯托尔)准备,试图获得它被纳入 C++ 标准,可能会被纳入 2014 年计划的修订版。我的实现是我日常工作的一部分,我无法决定它是否可以公开提供,尽管有一些希望可以在某个时候开源。

原文由 Dietmar Kühl 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用易于使用的带有模板的 C++ 仅标头解决方案: https ://github.com/vpiotr/decimal_for_cpp

请注意,这不是 *Big *Decimal 类;它仅限于 64 位的“尾数”数字。

[取自链接]

   #include "decimal.h"

  using namespace dec;

  // the following declares currency variable with 2 decimal points
  // initialized with integer value (can be also floating-point)
  decimal<2> value(143125);

  // to use non-decimal constants you need to convert them to decimal
  value = value / decimal_cast<2>(333.0);

  // output values
  cout << "Result is: " << value << endl;
  // this should display something like "429.80"

  // to mix decimals with different precision use decimal_cast
  decimal<6> exchangeRate(12.1234);
  value = decimal_cast<2>(decimal_cast<6>(value) * exchangeRate);

  cout << "Result 2 is: " << value << endl;
  // this should display something like "5210.64"

  cout << "Result 2<6> is: " << decimal_cast<6>(value) << endl;
  // this should display something like "5210.640000"

原文由 Piotr 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题