c++ - 如何打印出 DECIMAL (tagDEC) 类型的值?

标签 c++ windows

我必须使用 DECIMAL 类型(tagDEC 结构)以获得更高的精度。

但是 Windows API 中的 C++ 的 DECIMAL 只是一个结构。

typedef struct tagDEC {
    USHORT wReserved;
    union {
        struct {
            BYTE scale;
            BYTE sign;
        } DUMMYSTRUCTNAME;
        USHORT signscale;
    } DUMMYUNIONNAME;
    ULONG Hi32;
    union {
        struct {
            ULONG Lo32;
            ULONG Mid32;
        } DUMMYSTRUCTNAME2;
        ULONGLONG Lo64;
    } DUMMYUNIONNAME2;
} DECIMAL;

该值来自具有 .net DECIMAL 结构的对等方(C# 程序),通过 tcp 套接字。

那么如何以友好的方式显示/打印出它的值,例如 C++ 中的 - 12,345.678 * 10^(-21)?

最佳答案

使用一个简单的任意精度库(我找到了 this one ,自由许可,仅 header ),这应该是这样的:

DECIMAL input;
std::vector<Num::word> words{input.Lo64, input.Hi32};
Num n = Num(words.data(), words.data()+2, input.sign == 1).truncate() * Num(10).pow(input.scale);

std::vector<char> output;
n.print(output);
std::cout << output.data() << endl;

关于c++ - 如何打印出 DECIMAL (tagDEC) 类型的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57410527/

相关文章:

windows - 如何在 VisualSVN 中设置提交后电子邮件

c++ - 在链表程序中使用模板时在 C++ 中重载 << 运算符

c++ - 在 BOOST 中使用通用图像库时是否应该安装 jpeg 库

c++ - 为什么 `std::compare_three_way` 不是 `std::strict_weak_order` ?

c++ - 试图理解模板

c++ - Goto 无法正常工作 C++

windows - 无法从命令提示符打开 Jupyter 笔记本

C++ TCP 套接字插件

python - 启动 jupyter notebook 时出现 fatal error

c++ - qmake 为 win32-g++ 平台生成的 Makefile 包含 unix 命令