你可能会这样做(并忽略那篇文章):
#include <iomanip>
#include <iostream>
#include <sstream>
int main() {
// Environment
std::cout << "Global Locale: " << std::locale().name() << std::endl;
std::cout << "System Locale: " << std::locale("").name() << std::endl;
// Set the global locale (To ensure it is English in this example,
// it is not "")
std::locale::global(std::locale("en_GB.utf8"));
std::cout << "Global Locale: " << std::locale().name() << std::endl;
// Conversion string to double
std::istringstream s("108,457,000.89");
double d = 0;
s >> d;
// Conversion double to string
std::cout << std::fixed << d << std::endl;
// This stream (initialized before main) has the "C" locale,
// set it to the current global:
std::locale c = std::cout.imbue(std::locale());
std::cout << "Locale changed from " << c.name()
<< " to " << std::cout.getloc().name() << std::endl;
std::cout << std::fixed << d << std::endl;
return 0;
}
笔记:
- 在终端/控制台中运行它(我的开发环境 Eclipse 具有 C 语言环境)
- 您可能需要调整“en_GB.utf8”
结果:
Global Locale: C
System Locale: en_US.UTF-8
Global Locale: en_GB.utf8
108457000.890000
Locale changed from C to en_GB.utf8
108,457,000.890000
一个警告:
Libraries may rely on the global local being the "C" local.