Solved

COleCurrency formatting

Posted on 1997-08-19
6
790 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

719 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