- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在使用 money_get<> 解析 USD 和 $ 金额时遇到错误。
#include <iostream> /// cin, cout
#include <locale>
#include <iterator>
using namespace std;
int main()
{
cin.imbue(std::locale("en_US.UTF-8"));
std::cout << "USD 1.11$2.22"
<< " parsed with the facet directly: ";
auto& f = std::use_facet<std::money_get<char>>(cin.getloc());
std::ios_base::iostate err;
std::istreambuf_iterator<char> frm(cin), end;
long double val;
/// international currency symbol
frm = f.get(frm, end, true, cin, err, val);
std::cout << "\n "<< val/100;
string str;
cin >> str;
cout << "\n remaining: " << str << endl;
/// local currency symbol
f.get(frm, end, false, cin, err, val);
std::cout << "\n "<< val/100;
}
http://coliru.stacked-crooked.com/a/b1f76385cbed6834
输入是:
USD 1.11
$2.22
输出是:
USD 1.11$2.22 parsed with the facet directly:
1.11
remaining: .22
0
来自相应 moneypunct<> 方面的模式是: 对于美元:符号空间值 对于 $: sign symbol value none
显然,当读取第一个数量时,我超出了标记。我不知道为什么。谢谢。
最佳答案
我进一步隔离了问题。
这似乎不是 get_money() 操纵器或 money_get<> 方面的问题。
这是字符串 v/s cin 的问题 - 使用本地货币(例如:$)指定的货币金额的输入在 istringstream 中工作正常,但在 cin 中不工作。
请注意,我已经修改了来自 cppreference.com 的以下部分代码。
http://coliru.stacked-crooked.com/a/19e0fdc663c5d845
使用本地货币(例如:$)指定的货币金额输入在 istringstream 中正常工作:
string str = "$1.11 $2.22 $3.33 4.44 5.55";
void stringInput() /// works
{
cout << "string Input ... works:\n";
istringstream s1(str);
s1.imbue(locale("en_US.UTF-8"));
cout << fixed << setprecision(2);
cout << '"' << str << "\" parsed with the I/O manipulator: ";
long double val;
while(s1 >> get_money(val))
cout << val/100 << ' ';
cout << "\n";
}
输出:
string Input ... works:
"$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
使用本地货币(例如:$)指定的货币金额输入在 cin(相同链接)中不正确:
void cinInput() /// does not work
{
cout << "cin Input ... doesn't work:\n";
cin.imbue(locale("en_US.UTF-8"));
cout << fixed << setprecision(2);
cout << '"' << str << "\" parsed with the I/O manipulator: ";
long double val;
while(cin >> get_money(val))
std::cout << val/100 << ' ';
cout << '\n';
}
输出:
cin Input ... doesn't work:
"$1.11 $2.22 $3.33 4.44 5.55" parsed with the I/O manipulator: 0.11 0.22 0.33 0.44 0.55
关于c++ - 使用 money_get<> 解析 USD 和 $ 金额时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48302667/
我正在尝试创建一个可以识别金额(美元)的正则表达式。问题是数据是在扫描的 PDF 文件上通过 OCR 生成的,因此数据不精确: $可以用S表示 .可以表示为, 1可以用l或I表示 5可以用S表示 例子
在写输入用到input的时候,经常出现以下几种情况: 只能输入某。栗子:只能输入数字,只能输入字母(大写,小写)只能输入某固定格式。栗子:只能输入金额,只能输入小数且最多保留2位不能输入某。栗子:
我们正在开发旅游网站,用于预订航类、酒店、汽车等。它是基于产品的软件。客户(购买我们软件的)将成为“主要代理机构”。他的总交易将以 INR(印度卢比货币)为单位。航类、酒店或汽车预订总额仅以“印度卢比
以下操作有什么区别吗? (将当前日期提前 160 天) Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_WEEK,
假设有 5 个桶 (1 - 5),并且为每个桶分配一个(整数)值。例如 > bucket = 1:5 > value = c(14, 12, 9, 20, 7) > data.frame(bucket
我正在尝试使用 MYSQL 查询对每月和每年以及该月的总收入进行分组,我尝试了多次,但似乎总是出错。 我当前的查询: Month Year
我正在创建一个 PHP 表单,其中包含客户的信息和他们想要花费的金额,当点击提交按钮时,详细信息将发送到我的电子邮件地址。 这就是我遇到的问题。然后我想向他们发送指向 PayPal 帐户的链接以完成购
我想获取总交易量超过50000的用户 SELECT sum(tractions.amount) as total , user_id FROM `tractions` where `total`
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我有一段固定的文本,每次文本进入滚动的 div 时我都会尝试添加不同的类。我用起来没问题。但是如果我向固定文本添加偏移量,例如 top: 400px 我需要在 JS 中抵消这个偏移量。但我似乎无法弄清
我有下表 create table supplier_paid_details( id bigint(10) NOT NULL AUTO_INCREMENT, payment_mode
我正在练习 java 银行帐户中的一些简单任务,需要有关代码块的建议,如何编写?这是一个例子,如果用户输入字符串或一些字母而不是数字来打印“请输入数字”,如何输入 Else if block amou
我正在使用 angularJS 在 mvc-5 中创建一个基于 Web 的应用程序,我在表中得到了金额总和 Total: {{totalAmount}} 我像这样从 Controller 获取金额 $
我的数据就是这样返回的。我需要返回列表中的所有值,确保时间格式和票价金额按照我的解释进行纠正。我想删除票价中的逗号以及出发和到达中的 AM & PM。提前谢谢了。因为大约有 3 个航类代码,总共有 1
我想计算一下 数量 * 比率 = 金额 金额 - 折扣 + 税费 = 账单金额 账单金额+四舍五入= Netty 我知道这很容易完成,但问题是任何人都可以通过检查元素更改该值。为了阻止这种情况,我必须
基本上,我正在 LINQ 中寻找一种方法来选择列表中的第一个(比如说 3 个)分组对象。 例如,列表可能包含: {“AAA”、“AAA”、“AAA”、“AAA”、“BBB”、“BBB”、“CCC”、“
我正在尝试按类别对金额进行求和,但存在基于引用编号的重复金额,并且我只想为每个引用包含 1 个金额。大约有100K个不同的引用号,全线有4个差异量。 我正在分析的数据如下所示: reference |
我有一个表,每个 user_id 有很多行 我正在尝试按 user_id 对行进行分组并对它们的金额进行求和 这是表结构 Name Type Collation Attributes
如何查询实际金额 1.00 以内的金额列? 例如,如果 AmountPaid = 7.75,我想返回 Amount 在 6.75 - 8.75 之间的所有结果。 我知道我忽略了一些简单的事情,但到目前
我是 C# 的新手,正在为我尝试创建的程序而苦苦挣扎。我希望我能尽我所能提出这个问题。根据我的任务,我们将在 Visual Basic 中创建一个用于创建帐户的 Windows 窗体。出于我的问题的目
我是一名优秀的程序员,十分优秀!