From: Ashish on 10 Feb 2010 02:45 sprintf works fine when convert a double to string char array[255]; memset(array,0,255); float value = 2.5432; sprintf(array,"%.2f",value ); array will contain 2.54 But when i use WCHAR* type then sprintf fail to put value in WCHAR array. WCHAR warray[255]; memset(warray,0,sizeof(warray)); wsprintfW(warray,L"%.2f",value); warray will contain .2f Please suggest how to do that.
From: David Lowndes on 10 Feb 2010 03:14 >But when i use WCHAR* type then sprintf fail to put value in WCHAR array. >WCHAR warray[255]; >memset(warray,0,sizeof(warray)); >wsprintfW(warray,L"%.2f",value); > >warray will contain .2f wsprintf does not support float or double types. >Please suggest how to do that. swprintf Dave
From: Joseph M. Newcomer on 10 Feb 2010 16:42 What is wrong with this code? See below... On Wed, 10 Feb 2010 13:15:39 +0530, "Ashish" <akohli_2004(a)hotmail.com> wrote: >sprintf works fine when convert a double to string >char array[255]; **** (a) it uses the obsolete data type 'char' (b) it is a fixed-size array **** >memset(array,0,255); **** This is completely silly. At the very best, the re-use of 255 is bad practice, but can you explain why it is necessary to clear out a variable that you are about to overwrite? This makes as much sense as writing int i; i = 0; // clear it out i = 2; // now set it to 2 Unfortunately, while an even slightly intelligent compiler can eliminate the silly extra assignment, a compiler is not permitted to delete the memset, thinking that the programmer actually had a clue that it made sense. It doesn't. **** >float value = 2.5432; >sprintf(array,"%.2f",value ); **** This is considered dangerous, and a firable offense in some companies. You must never think of sprintf as being a viable way to EVER format ANYTHING. Strictly speaking, since this is an MFC forum, the correct solution is CString s; s.Format(_T("%.2f", value); and THAT'S ALL; it is safe, and it works correctly. But if you feel this retro desire to work in obsolete programming models, the correct solution is _tsprintf_s(array, sizeof(array) / sizeof(TCHAR), _T("%.2f"), value); or _tsprintf_s(array, _countof(array), _T("%.2f"), value); But forget you ever heard of sprintf. It is thoroughly dead as a piece of technology. **** > >array will contain 2.54 > >But when i use WCHAR* type then sprintf fail to put value in WCHAR array. >WCHAR warray[255]; >memset(warray,0,sizeof(warray)); >wsprintfW(warray,L"%.2f",value); > >warray will contain .2f >Please suggest how to do that. **** In older versions of the world, wsprintf would have required calling a Unicode formatter, but there was no wtof function to convert floating point to string, so it cannot work. For some reason, Microsoft neglected to think about Unicode users needing to format floating point numbers! If you really, really have to do this, you have to kludge it, first converting the string with ANSI APIs and using the string in a CString constructor to convert it to Unicode. It is really, really bad to do this for a lot of reasons. For example: CStringA fpnum; fpnum.Format("%.2f", value); CStringW s(fpnum); You did not mention which version of VS you are using. But in any case, you must not use wsprintf for all the reasons you must not use sprintf. Like sprintf, it is a piece of dead technology that should join punched cards in the dustbin of history. joe **** > > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on 10 Feb 2010 16:52 I just saw that this discussion was about wsprintf, not swprintf, which is the wide-character version of sprintf. wsprintf is an obsolete Win16 API, WHICH SHOULD NEVER BE USED FOR ANY PURPOSE WHATSOEVER AT ANY TIME! Consider it well and truly dead, and pretend you never heard of it. joe On Wed, 10 Feb 2010 13:15:39 +0530, "Ashish" <akohli_2004(a)hotmail.com> wrote: >sprintf works fine when convert a double to string >char array[255]; >memset(array,0,255); >float value = 2.5432; >sprintf(array,"%.2f",value ); > >array will contain 2.54 > >But when i use WCHAR* type then sprintf fail to put value in WCHAR array. >WCHAR warray[255]; >memset(warray,0,sizeof(warray)); >wsprintfW(warray,L"%.2f",value); > >warray will contain .2f >Please suggest how to do that. > > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
|
Pages: 1 Prev: Adding controls to a layered window Next: How to get device name? |