money_get<>工作正常吗?

Does money_get<> work correctly?

本文关键字:常吗 工作 gt lt money get      更新时间:2023-10-16

在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
相关文章: