如何打印出十进制(tagDEC)类型的值?

How to print out the value of a DECIMAL (tagDEC) type?

本文关键字:类型 tagDEC 何打印 打印 十进制      更新时间:2023-10-16

我必须使用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;

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

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

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

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;