conversion of long to LPCTSTR

void CODBCView::OnDraw(CDC* pDC)
      // TODO: Add your specialized code here and/or call the base class
      CRect oRect;
      CString m_strPartID;
      long iRowCount = 0;

      m_DBGrid.MoveWindow(oRect, TRUE);

      m_DBGrid.SetRows(m_pSet->GetRecordCount() + 1);

      m_DBGrid.SetColWidth(-1, 1440);

      m_DBGrid.SetText("Part ID");
      m_DBGrid.SetText("Part Name");
      m_DBGrid.SetText("Part Description");


      iRowCount += 1;

      while (!m_pSet->IsEOF()) {
            iRowCount += 1;


I have an Access97 database. One of my fields is PartID (of autonumber type). How do I convert PartID to LPCTSTR so that it can be displayed using the built-in "SetText()" function in Visual C++.

I have another question. Is there any way to do currency formatting in Visual C++. For example,


where UnitPrice is of type currency. I wish to format it to display up to 2 decimal places.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi.. can you please tell me what type is the m_DBGrid.SetText("Part ID");
i mean.. is it a UNICODE string like LPWSTR or something else???

 Here is probably what you need.
For normal strings:

 char *_ltoa( long value, char *string, int radix );

For Wide Character Strings:
wchar_t *_ltow( long value, wchar_t *string, int radix );

Routine Required Header Compatibility
_ltoa <stdlib.h> Win 95, Win NT
_ltow <stdlib.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.


LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns a pointer to string. There is no error return.



Number to be converted


String result


Base of value


The _ltoa function converts the digits of value to a null-terminated character string and stores the result (up to 33 bytes) in string. The radix argument specifies the base of value, which must be in the range 2 – 36. If radix equals 10 and value is negative, the first character of the stored string is the minus sign (–). _ltow is a wide-character version of _ltoa; the second argument and return value of _ltow are wide-character strings. Each of these functions is Microsoft-specific.

Generic-Text Routine Mappings

Feel free to ask

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 Probably here is what you need to do,

 // you have the long PartId.
// have this in the header
// init to null in constructor
 LPSTR  m_lpszPartId ;

// and then allcate memory
m_lpszPartId = new char[1000];

// convert long to string
m_lpszPartId = _ltoa(PartId, m_lpszPartId, 10);

// display

Regarding your second question there should be no problem displaying the $ but other currencies display i do not know. Will try and find.

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

This is way to simple.

First use

m_strPartID.Format( "%ld", m_pSet->m_PartID );

then you call SetText( m_strPartID );

You can use the format function to format currency

m_strPrice( "$%.2f", m_pSet->m_UnitPrice );

This is way easyer than what you have received before.

If this is the best answer you can accept the comment as answer.

Hope this helps

A typo in my erlier comment.
Should be.

m_strPrice.Format( "$%.2f", m_pSet->m_UnitPrice );

If you want I can explain how CString.Format works or you can check the help.

ervinlohAuthor Commented:
Thanks qvq and abdij.

To qvq:
I have checked through the MSDN Visual C++ help for the explanation. Thanks anyway.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.