fpi*_*o07 36 c++ floating-point double types decimal
有没有办法使用十进制数据类型,比如一种方式decimal32,decimal64或者decimal128在我的C++程序?
Die*_*ühl 27
Decimal TR中的类未针对所有编译器实现.一些编译器,例如gcc,实现了C Decimal TR,并在C++中提供了相应的扩展.过去有一个C++ Decimal TR的开源实现可用,但我找不到它.如果您的编译器不支持十进制类型,那么最好的选择可能是为IBM的decNumber库创建一个包装器.
为了改善C++未来的情况,我已经创建了一个更新TR的计划,我将把当前的TR变成一个完整的提案,为下一次C++委员会会议做准备(4月在布里斯托尔),试图获得它被采用到C++标准中,可能进入2014年计划的修订版.我的实现是我常规工作的一部分,我不应该决定它是否可以公开发布,尽管有一些希望它可以在某个时候开源.
小智 15
您可以使用易于使用的带有模板的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"
Run Code Online (Sandbox Code Playgroud)