虽然时间已经很晚,但它可以为未来的读者提供一些见解。 按照上文Florian Kusche的答复模式,这里完全是“C”替代物,我已经成功地在各种护堤上进行了测试。 这一解决办法的一项要求是在实施<代码>strtod(之前暂时取消当地用户的系统。
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _LOCALE_N_ 13
int category[_LOCALE_N_] =
{
LC_ALL , // All of the locale
LC_ADDRESS , // Formatting of addresses and geography-related items (*)
LC_COLLATE , // String collation
LC_CTYPE , // Character classification
LC_IDENTIFICATION, // Metadata describing the locale (*)
LC_MEASUREMENT , // Settings related to measurements (metric versus US customary) (*)
LC_MESSAGES , // Localizable natural-language messages
LC_MONETARY , // Formatting of monetary values
LC_NAME , // Formatting of salutations for persons (*)
LC_NUMERIC , // Formatting of nonmonetary numeric values
LC_PAPER , // Settings related to the standard paper size (*)
LC_TELEPHONE , // Formats to be used with telephone services (*)
LC_TIME // Formatting of date and time values
};
void _store_locale_info(char *vals[_LOCALE_N_])
{
/* store the current locale information in an array of strings for future use */
int i;
for (i=0; i<_LOCALE_N_; i++)
{
char *loc_str = setlocale(category[i], "");
int L = strlen(loc_str);
vals[i] = calloc(L+1, sizeof(char));
strncpy(vals[i], setlocale(category[i], ""), L+1);
}
}
void _restore_locale_info(char *vals[_LOCALE_N_])
{
/* restore the locale information from a previosly-populated array of strings */
int i;
for (i=0; i<_LOCALE_N_; i++)
{
if (vals[i])
{
setlocale(category[i], vals[i]);
free(vals[i]);
}
}
}
double _strtod_c (const char *string, char **endPtr)
{
/* Wrapper function for strtod() that enforces the "C" locale before converting a floating-point
* number from an ASCII decimal representation to internal double-precision format. */
char *vals[_LOCALE_N_];
double rval = 0;
_store_locale_info(vals);
rval = strtod(string, endPtr);
_restore_locale_info(vals);
return rval;
}
int main()
{
char *str = "1024.123456";
char **endPtr;
char locale_str[100];
printf("
str = "%s"
", str);
printf("Locale
", str);
strcpy(locale_str, setlocale(LC_ALL, "C"));
printf("%-6s :: strtod(str, endPtr) = %.15g
", locale_str, strtod(str, endPtr));
strcpy(locale_str, setlocale(LC_ALL, "de_DE"));
printf("%-6s :: strtod(str, endPtr) = %.15g
", locale_str, strtod(str, endPtr));
printf("---
");
strcpy(locale_str, setlocale(LC_ALL, "C"));
printf("%-6s :: _strtod_c(str, endPtr) = %.15g
", locale_str, _strtod_c(str, endPtr));
strcpy(locale_str, setlocale(LC_ALL, "de_DE"));
printf("%-6s :: _strtod_c(str, endPtr) = %.15g
", locale_str, _strtod_c(str, endPtr));
printf("
");
}
相容的含水层安装的预期产出是
str = "1024.123456"
Locale
C :: strtod(str, endPtr) = 1024.123456
de_DE :: strtod(str, endPtr) = 1024
---
C :: _strtod_c(str, endPtr) = 1024.123456
de_DE :: _strtod_c(str, endPtr) = 1024.123456