money_get<>工作正常吗?
Does money_get<> work correctly?
在coliru上,我已经测试了美元和欧元的money_get<>方面。
它适用于美元,而不是欧元,它工作正常(就我的测试数据而言)。
测试美元的程序是:
int main()
{
std::string str = "$1.11 $2.22 $3.33 4.44 5.55";
std::istringstream s1(str);
s1.imbue(std::locale("en_US.UTF-8"));
std::cout << std::fixed << std::setprecision(2);
std::cout << '"' << str << "" parsed with the I/O manipulator: ";
long double val;
while(s1 >> std::get_money(val))
std::cout << val/100 << ' ';
std::cout << 'n';
str = "USD 1,234.56";
std::istringstream s2(str);
s2.imbue(std::locale("en_US.UTF-8"));
std::cout << '"' << str << "" parsed with the facet directly: ";
auto& f = std::use_facet<std::money_get<char>>(s2.getloc());
std::ios_base::iostate err;
std::istreambuf_iterator<char> beg(s2), end;
f.get(beg, end, true, s2, err, val);
std::cout << val/100 << 'n';
}
http://coliru.stacked-crooked.com/a/be545f641718040e
O/P 是正确的:
"$1.11 $2.22 $3.33 4.44 5.55" parsed with the I/O manipulator: 1.11 2.22 3.33 4.44 5.55
"USD 1,234.56" parsed with the facet directly: 1234.56
测试欧元的程序类似:
int main()
{
std::string str = "1,11 2,22 3,33 € 456789,12 €";
std::istringstream s1(str);
s1.imbue(std::locale("de_DE.utf8"));
std::cout << std::fixed << std::setprecision(2);
std::cout << '"' << str << "" parsed with the I/O manipulator: ";
long double val;
while(s1 >> std::get_money(val))
std::cout << val/100 << ' ';
std::cout << 'n';
str = "1.234,56 EUR";
std::istringstream s2(str);
s2.imbue(std::locale("de_DE.utf8"));
std::cout << '"' << str << "" parsed with the facet directly: ";
auto& f = std::use_facet<std::money_get<char>>(s2.getloc());
std::ios_base::iostate err;
std::istreambuf_iterator<char> beg(s2), end;
f.get(beg, end, true, s2, err, val);
std::cout << val/100 << 'n';
}
http://coliru.stacked-crooked.com/a/a91720a9dbd4eb5e
O/P 是错误的:
"1,11 2,22 3,33 € 456789,12 €" parsed with the I/O manipulator: 1.11 2.22 3.33
"1.234,56 EUR" parsed with the facet directly: 1234.56
请注意,不会打印第 4 欧元金额。这是因为一旦 money_get<>.get() 函数遇到第 3 个值中的 € 符号,就会给出错误。它只处理没有€符号的普通金额。
德语区域设置的 coliru 上的money_punct<> 方面给出了以下定义:
moneypunct in locale "de_DE.utf8":
decimal_point: ,
thousands_sep: .
grouping: 3 3
curr_symbol: €
positive_sign:
negative_sign: -
frac_digits: 2
pos_format: sign value space symbol
neg_format: sign value space symbol
请注意,我已按照neg_format的规定正确输入了欧元金额。
如何解决这样的问题?
谢谢。
正如 std::money_get:get 的 cpppreferences 页面上提到的,std::showbase
是使结束位置的货币符号非可选所必需的:
std::string str = "3,33 € 456789,12 €";
std::istringstream s1(str);
s1.imbue(std::locale("de_DE.utf8"));
std::cout << std::fixed << std::setprecision(2);
std::cout << '"' << str << "" parsed with the I/O manipulator: ";
long double val;
s1 >> std::showbase; // <-- this
while(s1 >> std::get_money(val))
std::cout << val/100 << ' ';
std::cout << 'n';
科里鲁的输出
"3,33 € 456789,12 €" parsed with the I/O manipulator: 3.33 456789.12
是的,你是对的,库比。
根据您提供的信息,我已经编写了程序,并解释了为什么会发生这种情况。
1) 读取美元金额的程序已经在工作。那里没有问题。
http://coliru.stacked-crooked.com/a/be545f641718040e
2) 要读取欧元金额,您必须设置输入流的 showbase 标志,如 Cubbi 所示。
当所涉及的欧元金额使用当地符号€时,这就是所有必要的。
但是,当涉及的欧元金额使用国际符号EUR时,读取数据中的符号必须是4个字符的字符串"EUR"。最后一个空间很重要,否则不会被占用。这可能是因为国际表示形式是一个 4 个字符的 C 样式字符串,最后一个字符是终止零。[Stroustrup,"C++编程语言",第4版,第1136页。
http://coliru.stacked-crooked.com/a/ef38fb1d1c98aa22
/// ...
str = "1.234,56 EUR ";
std::istringstream s2(str);
s2.imbue(std::locale("de_DE.utf8"));
// s2 >> std::showbase;
std::cout << '"' << str << "" parsed with the facet directly: ";
auto& f = std::use_facet<std::money_get<char>>(s2.getloc());
std::ios_base::iostate err;
std::istreambuf_iterator<char> beg(s2), end;
f.get(beg, end, true, s2, err, val);
std::cout << val/100 << 'n';
"1.234,56 EUR " parsed with the facet directly: 1234.56
3) 同样,要读取INR金额,您必须设置输入流的显示基础标志。
当涉及的 INR 金额使用本地符号 ₹ 时,这就是所有必要的。
但是,当涉及的INR金额使用国际符号INR时,读取数据中的符号必须是4个字符的字符串"INR"。最后一个空间很重要,否则不会被占用。
http://coliru.stacked-crooked.com/a/216cc78592141ae1
/// ...
str = "INR 100,234.56";
std::istringstream s2(str);
s2.imbue(std::locale("en_IN"));
// s2 >> std::showbase;
std::cout << '"' << str << "" parsed with the facet directly: ";
auto& f = std::use_facet<std::money_get<char>>(s2.getloc());
std::ios_base::iostate err;
std::istreambuf_iterator<char> beg(s2), end;
f.get(beg, end, true, s2, err, val);
std::cout << val/100 << 'n';
请注意,数据字符串中有 2 个相邻空格:1 个在 INR 符号之后,1 个在值之前。这是模式 {符号符号空间值} 所必需的。
"INR 100,234.56" parsed with the facet directly: 100234.56
- 导入库可以跨dll版本工作吗
- 有人能解释一下为什么下界是这样工作的吗C++的
- 范围 v3 可以包含初始值设定项列表的工作吗?
- #if 如何工作?我误解了 #if~#endif 的形式吗?
- 有人可以解释一下这个矩阵幂函数是如何工作的吗?
- 有人可以向我解释这个链表是如何工作的吗?
- 有人可以详细解释这个回文代码是如何工作的吗?
- MPI_Reduce也是MPI_Barrier的工作吗?
- cout 在我的程序上无法正常工作,有人可以帮助我吗?
- ostream_iterator运算符=在pair<int,int>上失败,但在包装器类上工作。我不能为成对<>重载运算符<<吗?
- tbb::enumerable_thread_specific在其他线程库中工作吗
- wait_until在主线程而不是主线程中的工作方式不同吗? c ++
- 有人可以解释速记赋值运算符的实际工作原理吗?
- "= 删除"如何工作?有人可以解释这个构造函数吗?
- 我可以更改将引用参数传递到指针中并使其正常工作的函数的输入(C、C++)吗?
- 尝试使用 Qt 库中的 QPixmap 将图像拆分为多个块。关于他的复制方法的工作方式,我有什么不明白的吗?
- Windows API "CreateWindowEx" 在 Python (3.6.8) 64 位使用的 DLL 中工作吗?
- Qt拖放在窗口中工作.我可以做到我只能放入Qlabel而不是整个窗口吗?
- 有人可以向我解释一下这段代码是如何工作的吗?(程序在预期数值且用户输入其他输入时再次要求用户输入)
- money_get<>工作正常吗?