Solved

COleCurrency formatting

Posted on 1997-08-19
6
774 Views
Last Modified: 2013-11-19
I am using COleCurrency linked via DDX_Text to edit controls in dialog boxes. Unfortunately, when displaying the value COleCurrency does not format the string as specified in Control Panel International settings. I would like to get always two digits after decimal point. But when the value stored in COleCurrency is integer, it does not add the comma and two zeros.

How can I achieve the proper formatting of COleCurrency?
0
Comment
Question by:czarekn
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 1304307
Use COleCurrency::Format().  Return value is  CString that contains the formatted currency value.

Call this member function to create a formatted representation of the currency value. It formats the value using the national language specifications (locale IDs). A currency symbol is not included in the value returned. If the status of this COleCurrency object is null, the return value is an empty string. If the status is invalid, the return string is specified by the string resource IDS_INVALID_CURRENCY.

Example




COleCurrency curA;           // value: 0.0000
curA.SetCurrency(4, 500);    // value: 4.0500

// value returned: 4.05
curA.Format(0, MAKELCID(MAKELANGID(LANG_CHINESE,
     SUBLANG_CHINESE_SINGAPORE), SORT_DEFAULT));
// value returned: 4,05
curA.Format(0, MAKELCID(MAKELANGID(LANG_GERMAN,
     SUBLANG_GERMAN_AUSTRIAN), SORT_DEFAULT));
0
 

Author Comment

by:czarekn
ID: 1304308
Well, I know how to read the help files but it seems that you can't read the question.

Linking a COleCurrency object with a control in a dialog is done via DDX_Text function that uses COleCurrency::Format () method to display the value. Unfortunately Format() does not take into account the settings of Control Panel.

If I have a currency of value 4.5 it gets formatted as "4.5" not "4.50" as settings in CP would suggest. Then if I have 4.51 it gets formatted as "4.51" and 4.511 as "4.511" which spoils right align of numbers.  

Could you please remedy this?

0
 

Expert Comment

by:bss12
ID: 1304309
You can try to set Your project language settings from US English to for instance German, maybe it will work with no additional code, only via DDX.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:czarekn
ID: 1304310
My project's settings seem to be all right. The only place where a polish language (this is the country I am from) can be set is the Resource tab of projects settings. and it is set to Polish. Even MFC messages such as "Please enter currency." are in Polish.

Do you get proper formatting? With all the zero's if the currency is integer?

I get the proper formatting in Access so somehow it should work.


0
 

Accepted Solution

by:
bss12 earned 200 total points
ID: 1304311
Sorry, no way, the German settings in project do not help. The whole stuff (I mean the DDX_Text) does not work properly.
The steps:
1. go to DoDataExchange and throw something like:
DDX_Text(pDX, IDC_YOUR_EDIT, m_your_currency_member);
away!!!
2. Somewhere outside from
//}}AFX_DATA_MAP
write:
HWND hWndCtrl= pDX->PrepareEditCtrl(IDC_YOUR_EDIT);
if (!pDX->m_bSaveAndValidate)
{
  float val = (float) (m_your_currency_member.m_cur.int64)/10000;
  CString str; str.Format("%.2f", val);
  ::SetWindowText(hWndCtrl, str);
}
else
{
  int nLen = ::GetWindowTextLength(hWndCtrl);
  CString strTemp;
  ::GetWindowText(hWndCtrl, strTemp.GetBufferSetLength(nLen), nLen+1);
  strTemp.ReleaseBuffer();
  if (!m_your_currency_member.ParseCurrency(strTemp))  // throws exception
  {
    // Can't convert string to currency
    AfxMessageBox(AFX_IDP_PARSE_CURRENCY);
    pDX->Fail();    // throws exception
  }
}

I have tested that, IT WORKS!!!!
If you wand to operate with the international settings (i.e. you want to internationalaze your app, retrieve the settings and set the appropriate format. I have used the german formatting (%.2f), i think it meets the Polish ones also.
Do widzenja! (I was in Poland in 1989)

Albert



0
 

Author Comment

by:czarekn
ID: 1304312
I was afraid I will have to do that. The problem lies in the acceptable value ranges for float, double and COleCurrency.

Thank you Albert.

Cezary N.

PS. It is not the fault of the DDX_Text. This is the COleCurrency::Format () member that DDX_Text uses. Format() simply spoils all that and there is no way to fix it as it is not virtual.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 37
Looking for a specific application/software 2 104
fix34  challenge 9 104
How to convert MFC::CString to UTF8 wchar_t* 10 141
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now