C language > Expert questions

Formatting numbers

(1/2) > >>

Grincheux:
I try to format decimal numbers using GetNumberFormat but the function always returns 0 => Error 87.


--- Code: ---static NUMBERFMTW NumberFormat = {3,1,3,L".",L" ",3} ;

LPSTR FormatNumberEx(LPSTR  __lpszValue,LPSTR __lpszNumberStr,int __cchNumber)
{
   wchar_t   _szLocaleName[LOCALE_NAME_MAX_LENGTH] ;
   wchar_t   _szWideChar[512] ;
   wchar_t   _szOutput[512] ;

   MultiByteToWideChar(CP_UTF8,MB_PRECOMPOSED,__lpszValue,-1,_szWideChar,sizeof(_szWideChar)*sizeof(TCHAR)) ;
   GetNumberFormatEx(LOCALE_NAME_USER_DEFAULT,0,_szWideChar,&NumberFormat,_szOutput,512) ;
   GetLastError() ;
   WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK,_szOutput,-1,__lpszNumberStr,__cchNumber,NULL,NULL) ;
   
   return (__lpszNumberStr) ;
}

--- End code ---

Could you help me?
Thanks

John Z:
Hi,

I haven't tried to run your code yet but I believe that where you have used MB_PRECOMPOSED should be changed to 0 (zero). 

https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar

"Note  For UTF-8 or code page 54936 (GB18030, starting with Windows Vista), dwFlags must be set to either 0 or MB_ERR_INVALID_CHARS. Otherwise, the function fails with ERROR_INVALID_FLAGS"

Might try that.  Could double check the MBTW by outputting the conversion into a messagebox to be sure.

Regards,
John Z

Grincheux:
I have modified my code and the only error that I get is after calling GetNumberFormatEx. It returns 0 and GetLastError returns 87 (INVALID PARAMETER).



--- Code: ---LPSTR FormatNumberEx(LPSTR  __lpszValue,LPSTR __lpszNumberStr,int __cchNumber)
{
   wchar_t   _szLocaleName[LOCALE_NAME_MAX_LENGTH] ;
   wchar_t   _szWideChar[512] ;
   wchar_t   _szOutput[512] ;

   MultiByteToWideChar(CP_UTF8,MB_PRECOMPOSED,__lpszValue,-1,_szWideChar,sizeof(_szWideChar)) ;
   GetLastError() ;
   GetUserDefaultLocaleName(_szLocaleName,LOCALE_NAME_MAX_LENGTH) ;
   GetLastError() ;
   GetNumberFormatEx(_szLocaleName,0,_szWideChar,&NumberFormat,_szOutput,512) ;
   GetLastError() ; // returns 87
   WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK,_szOutput,-1,__lpszNumberStr,__cchNumber,NULL,NULL) ;
   
   return (__lpszNumberStr) ;
}
--- End code ---

John Z:
Hi,

Can you post your input string to be converted?  I've tried a version and don't get any error up through

--- Code: ---   char __lpszValue[30]={"33.4455555"};// test input string
   wchar_t   _szLocaleName[LOCALE_NAME_MAX_LENGTH] ;
   wchar_t   _szWideChar[512] ;
   wchar_t   _szOutput[512] ;

   MultiByteToWideChar(CP_UTF8,0,__lpszValue,-1,_szWideChar,sizeof(_szWideChar)*sizeof(TCHAR)) ;
   GetNumberFormatEx(LOCALE_NAME_USER_DEFAULT,0,_szWideChar,&NumberFormat,_szOutput,512) ;
   GetLastError() ;
   MessageBoxW(gHWND,_szOutput,L"Results",MB_OK);// shows 33.446 for "33.4455555" input string

--- End code ---
the GetNumberFormatEx.  The Message Box shows the converted string . . . . .

John Z

Grincheux:
Thank You John.
I have found a solution :
--- Code: ---LPSTR FormatNumberEx(LPSTR  __lpszValue,LPSTR __lpszNumberStr,int __cchNumber)
{
   wchar_t   _szLocaleName[LOCALE_NAME_MAX_LENGTH] ;
   wchar_t   _szWideChar[512] ;
   wchar_t   _szOutput[512] ;

   while(*__lpszValue == ' ')
      __lpszValue++ ;

   MultiByteToWideChar(CP_UTF8,MB_PRECOMPOSED,__lpszValue,-1,_szWideChar,sizeof(_szWideChar)) ;
   GetUserDefaultLocaleName(_szLocaleName,LOCALE_NAME_MAX_LENGTH) ;
   GetNumberFormatEx(_szLocaleName,0,_szWideChar,NULL,_szOutput,512) ;
   WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,_szOutput,-1,__lpszNumberStr,__cchNumber,NULL,NULL) ;
   
   return (__lpszNumberStr) ;
}
--- End code ---


The result is joint to this post.
I had a problem because there was a white space before the numbers! >:( >:( >:( >:(

Navigation

[0] Message Index

[#] Next page

Go to full version