Solved

COleCurrency formatting

Posted on 1997-08-19
6
764 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

18 Experts available now in Live!

Get 1:1 Help Now