你可以做這樣的(而忽略文章):
#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.
在*雙*存儲這些類型的數字幾乎是從來不是一個錯誤。貨幣值應該存儲在使用base-10編碼的數據類型中,如C#的System.Decimal。在C++標準中不支持,你需要去購物。否則,在使用COleCurrency類的MSVC++中支持,請注意ParseCurrency()方法。 –
感謝您的評論,但這真的沒有幫助我用它來解析234,098.6700,它把它當作234.0000,它不能理解數字分組符號。我發現這篇文章也http://www.codeproject.com/Articles/9600/Windows-SetThreadLocale-and-CRT-setlocale直到現在我沒有讀到它,如果我發現任何解決方案,我會在這裏提到它 – ahmedsafan86