• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 846
  • Last Modified:

COleCurrency formatting

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
czarekn
Asked:
czarekn
  • 3
  • 2
1 Solution
 
gelbertCommented:
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
 
czareknAuthor Commented:
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
 
bss12Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
czareknAuthor Commented:
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
 
bss12Commented:
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
 
czareknAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now