user2436815
user2436815

Reputation: 3895

C++ converting Variant Decimal to Double Value

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

Upvotes: 1

Views: 3916

Answers (1)

Paolo Brandoli
Paolo Brandoli

Reputation: 4750

_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.

Upvotes: 3

Related Questions