C++ converting Variant Decimal to Double Value

試著忘記壹切 提交于 2019-12-11 09:58:52

问题


I have a _variant_t variable that contains a DECIMAL value from a database..

The problem is, I get an undefined value when I try to directly convert it to double type.

For example, I have a decimal value = 1000.111

_varaint_t MyVar = getValueFromDatabase();        // MyVar is a decimal value = 1000.111
double MyDouble = (double) MyVar.dblVal;

cout << MyDouble << endl;         // Prints "4.941204871279e-318#DEN"

What is the right way to convert Decimal to Double in C++?


UPDATE:

When I try this,

_varaint_t MyVar = getValueFromDatabase();        
double MyDouble = (double) MyVar.decVal;

I get a compile error:

No suitable conversion function from "DECIMAL" to "double" exists

回答1:


_variant_t already supplies the proper conversion operator, so you don't need to refer directly to .dblVal or decVal (see here: http://msdn.microsoft.com/en-us/library/ew0bcz27.aspx).

So you can do this:

_variant_t MyVar = getValueFromDatabase();
double myDouble = MyVar;

The conversion operator will call VariantChangeType if necessary.



来源:https://stackoverflow.com/questions/24891663/c-converting-variant-decimal-to-double-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!