在c \ c ++中存储COM的VT_DECIMAL的正确类型是什么? [英] What is the correct type in c\c++ to store a COM's VT_DECIMAL?
问题描述
我正在尝试为ADO写一个包装器.
当 VARIANT
类型为 VT_DECIMAL
时, DECIMAL
是 COM VARIANT
可以是的一种类型./p>
我正在尝试将其放入c本机数据类型,并保留变量值.似乎正确的类型是long double,但出现没有合适的转换错误".
例如:
_variant_t v;...if(v.vt == VT_DECIMAL){double d =(double)v;//这有效,但恐怕会丢失数据...长双倍ld1 =(长双倍)v;//错误:不止一次应用了从变体到长双精度的转换.长双倍ld2 =(长双倍)v.decVal;//错误:不存在合适的从十进制到long double的转换函数.}
所以我的问题是:
-
使用double来存储所有可能的十进制值是否绝对安全?
-
如果没有,如何将小数转换为长整数?
-
如何将小数转换为字符串?(使用<<运算符,
sprintf
也对我有好处)
DECIMAL
的内部表示形式不是双精度浮点值,而是带有符号/比例选项的整数.如果要初始化 DECIMAL
部分,则应初始化这些字段-96位整数值,小数位数,符号,然后获得有效的十进制 VARIANT
值.
DECIMAL
:
- scale-数字的小数位数.有效值为0到28.因此12.345表示为12345,小数位数为3.
- 符号-表示符号;0表示正数,DECIMAL_NEG表示负数.因此-1设置为DECIMAL_NEG时表示为1.
- Hi32-数字的高32位.
- Lo64-数字的低64位.这是_int64.
您的问题:
使用double来存储所有可能的十进制值是否绝对安全?
您不能直接将其初始化为double(例如 VT_R8
),但可以将其初始化为double变体,并使用变体转换API转换为 VT_DECIMAL
.可以对值进行较小的舍入.
如果没有,如何将小数转换为长整数?
如何将小数转换为字符串?(使用<<运算符,sprintf对我也有好处)
VariantChangeType
可以转换十进制变体到另一种类型的变体,包括整数,双精度型,字符串-您提供要转换为的类型.反之亦然,您还可以将其他内容转换为十进制.
I'm trying to write a wrapper to ADO.
A DECIMAL
is one type a COM VARIANT
can be, when the VARIANT
type is VT_DECIMAL
.
I'm trying to put it in c native data type, and keep the variable value. it seem that the correct type is long double, but I get "no suitable conversion error".
For example:
_variant_t v;
...
if(v.vt == VT_DECIMAL)
{
double d = (double)v; //this works but I'm afraid can be loss of data...
long double ld1 = (long double)v; //error: more then one conversion from variant to long double applied.
long double ld2 = (long double)v.decVal; //error: no suitable conversion function from decimal to long double exist.
}
So my questions are:
is it totally safe to use double to store all possible decimal values?
if not, how can I convert the decimal to a long double?
How to convert a decimal to string? (using the << operator,
sprintf
is also good for me)
The internal representation for DECIMAL
is not a double precision floating point value, it is integer instead with sign/scale options. If you are going to initialize DECIMAL
parts, you should initialize these fields - 96-bit integer value, scale, sign, then you get valid decimal VARIANT
value.
- scale - The number of decimal places for the number. Valid values are from 0 to 28. So 12.345 is represented as 12345 with a scale of 3.
- sign - Indicates the sign; 0 for positive numbers or DECIMAL_NEG for negative numbers. So -1 is represented as 1 with the DECIMAL_NEG bit set.
- Hi32 - The high 32 bits of the number.
- Lo64 - The low 64 bits of the number. This is an _int64.
Your questions:
is it totally safe to use double to store all possible decimal values?
You cannot initialize as double directly (e.g. VT_R8
), but you can initialize as double variant and use variant conversion API to convert to VT_DECIMAL
. A small rounding can be applied to value.
if not, how can I convert the decimal to a long double?
How to convert a decimal to string? (using the << operator, sprintf is also good for me)
VariantChangeType
can convert decimal variant to variant of another type, including integer, double, string - you provide the type to convert to. Vice versa, you can also convert something different to decimal.
这篇关于在c \ c ++中存储COM的VT_DECIMAL的正确类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!