-
July 25th, 2005, 08:10 PM
#1
Currency to double
I'm trying to convert a Currency value (Colecurrency data type ) to a double
The only thing I have found is to first convert the Currency value to a Cstring, via:
CString = COleCurrency::Format
then using atof to convert the Cstring to a real number.
It's a two step process, does anyone know if there is an easier way?
-
July 25th, 2005, 09:38 PM
#2
Re: Currency to double
Well if you really want to do it in a single line then you could try something like this (assuming a COleCurrency object called c):
Code:
double d = (double)((CURRENCY)c).int64 / 10000.0;
COleCurrency provides the means to cast to the CURRENCY type, which is a 64-bit integer scaled by 10,000. (It's actually defined as a union, so the currency value can be interpreted as a single 64-bit integer if that's supported, or as a struct with two separate 32-bit integers if need be. Hence the .int64 part of my code. See this MSDN entry for details.) So from there, you can cast the integer to a double and divide through by 10,000.
You should consider, though, whether this is a good idea. The reason CURRENCY is defined as it is is to avoid the inaccuracies that invariably turn up when working with floating-point numbers. (See this FAQ for lots of information.) So you should be careful when converting such a value to its floating-point equivalent; if you intend to use the floating point representations in any manner of calculations, then your program will be prone to the errors described in that FAQ.
-
July 25th, 2005, 10:38 PM
#3
Re: Currency to double
Thanks, this worked.
Another quick question:
I can't get the Currencey field to display two frational (cents ) values when the currency is a round amount, i.e, no cents.
For example if I want to display '100.00', the field always displays '100'.
I tried setting the currency to :
m_Curr1 = COleCurrency(100, 00);
But, it I can never get the .00 to show up. Can you help me?
-
July 26th, 2005, 08:25 AM
#4
Re: Currency to double
I assume that you could check if there are two decimal places and if there is not, you could just add a ".00" to then end of the displayed answer.
-
July 26th, 2005, 08:52 AM
#5
Re: Currency to double
Originally Posted by ADSOFT
For example if I want to display '100.00', the field always displays '100'.
.....
But, it I can never get the .00 to show up. Can you help me?
I already recommended you (in other thread) the simplest way to achieve it.
If you nevertheless, want to use your COleCurrency member variable and DDX_Text exchange mechanism - find thix API in MS source code:
Code:
void AFXAPI DDX_Text( CDataExchange* pDX, int nIDC, COleCurrency& value );
and overwrite it (write your own version) to display '100.00'...
However, (and you will see it!) it will be the same as to use the the simplest way but a bit longer...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|