NO

Author Topic: Formatting numbers  (Read 6548 times)

Grincheux

  • Guest
Formatting numbers
« on: October 29, 2020, 10:20:49 AM »
I try to format decimal numbers using GetNumberFormat but the function always returns 0 => Error 87.

Code: [Select]
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) ;
}

Could you help me?
Thanks
« Last Edit: October 29, 2020, 10:22:20 AM by Grincheux »

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Formatting numbers
« Reply #1 on: October 29, 2020, 02:38:06 PM »
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

  • Guest
Re: Formatting numbers
« Reply #2 on: October 29, 2020, 03:37:10 PM »
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: [Select]
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) ;
}

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Formatting numbers
« Reply #3 on: October 29, 2020, 04:47:04 PM »
Hi,

Can you post your input string to be converted?  I've tried a version and don't get any error up through
Code: [Select]
   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
the GetNumberFormatEx.  The Message Box shows the converted string . . . . .

John Z

Grincheux

  • Guest
Re: Formatting numbers
« Reply #4 on: October 29, 2020, 04:59:53 PM »
Thank You John.
I have found a solution :
Code: [Select]
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) ;
}


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


Offline John Z

  • Member
  • *
  • Posts: 860
Re: Formatting numbers
« Reply #5 on: October 29, 2020, 06:47:00 PM »
Glad you got it going, thumbs up.  If there is any possibility of trailing space be aware that too will fail.

Your output HTML looks great!

John Z