I try to format decimal numbers using GetNumberFormat but the function always returns 0 => Error 87.
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
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
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).
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) ;
}
Hi,
Can you post your input string to be converted? I've tried a version and don't get any error up through
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
Thank You John.
I have found a solution :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! >:( >:( >:( >:(
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