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
Solved

COleCurrency formatting

Posted on 1997-08-19
6
783 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 131
Expand data scrubbing tool 13 35
Detect CR LF to each line 12 170
Shell script to login to remote m/c and execute commands 10 78
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: 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…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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