Click here to Skip to main content
15,885,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query that gets the value of the payed amount of money. This field is currency type, in MS Access database.

I need to display this value in textbox ( I am using C++ and raw WinAPI for GUI ), so I need to know how to convert _variant_ from recordset into proper string (1,200.55).

Here is an example ( remember, I use raw WinAPI and C++ for GUI ):

C++
SetDlgItemText(hDlg, IDC_EDIT11, 
    pRS->Fields->GetItem(L"PaidValue")->Value.bstrVal); // problem is this line

My textbox is empty when I run the program.

When I debug it, it reports no errors.

QUESTION:

How can I convert _variant_t into string ( 1,200.00 )?
Posted

Won't the currency type be mapped to something a little less stringy? i.e. an R8? examine the
C++
Value->dblVal
component

As far as formatting it correctly, you have 4 locale items to determine (if you want to do it 'right')

1. currency symbol (LOCALE_SCURRENCY)
2. grouping (LOCALE_SMONGROUPING)
3. grouping seperator (LOCALE_SMONTHOUSANDSEP)
3. decimal seperator (LOCALE_SMONDECIMALSEP)
4. negation method (LOCALE_INEGCURR)

or just assume 3,comma grouping and . decimal (anglosphere type)

Easiest way is to print it to a string (%.2f) then carve it up manually to insert the comma at the right place
 
Share this answer
 
Currency values are stored as type CURRENCY in the cyVal member of the VARIANT[^] with variant type VT_CY. From the above link:
Quote:
A currency number is stored as 64-bit (8-byte), two's complement integer, scaled by 10,000 to give a fixed-point number with 15 digits to the left of the decimal point and 4 digits to the right. The value is in cyVal.

CURRENCY is defined as:
C++
typedef union FARSTRUCT tagCY {
    struct {
        unsigned long Lo;
        long          Hi;
    };
    LONGLONG int64;
} CY;


When using MFC, you can use the COleCurrency class which provides a format function. Without MFC you can implement similar functionality by checking how COleCurrency::Format is implemented (MFC source file olevar.cpp). It uses VarBstrFromCy[^] to convert the value to a BSTR.
 
Share this answer
 
Comments
AlwaysLearningNewStuff 11-Feb-15 12:24pm    
It did work, but it does not group thousands. I really need this functionality. I could use your solution to bet the string, but I do not know any function that formats it properly. Can you suggest one, since you are more experienced and better programmer than I am? Thank you for your help.
Jochen Arndt 11-Feb-15 12:55pm    
There is no ready-to-use function.

With C++ you can specify the format (example at http://cplus.about.com/od/learning1/ss/cppnumbers_5.htm). This can be also used with std::ostringstream to write the string to memory.

With C you must write your own function. An example can be found at http://www.cprogramming.com/snippets/source-code/thousand-separator-example (is C++ but can be adapted to print to a buffer).
Another example: http://www.drdobbs.com/cpp/a-simple-function-for-formatting-currenc/232700238

To avoid rounding errors you should not convert the value to double but split the 64-bit value it into digits before and after comma (div 10,000 / mod 10,000) and print the parts separately.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900