我对头文件中std::get_money定义的C ++函数感到困惑<iomanip>。get_money按编程概念的用途是什么?
我使用以下代码std::get_money。
#include <iostream> // std::cin, std::cout
#include <iomanip> // std::get_money
int main ()
{
long double price;
std::cout << "Please, enter the price: ";
std::cin >> std::get_money(price);
if (std::cin.fail()) std::cout << "Error reading price\n";
else std::cout << "The price entered is: " << price << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我输入100.25时,它返回100。输出和货币格式之间是什么关系?我阅读了此参考资料,但无法理解这种关系。同样的困惑存在与std::put_money,std::get_time,和std::put_time。
实际使用的例子有哪些?
这是我不知道存在的标准库的一部分!根据cppreference,您必须设置区域设置来定义时间和金钱的格式。这里我使用 en_US 语言环境。
#include <iomanip>
#include <iostream>
int main() {
long double price;
std::cin.imbue(std::locale("en_US.UTF-8"));
std::cout << "Please enter the price: ";
std::cin >> std::get_money(price);
if (std::cin.fail()) {
std::cout << "Error reading price\n";
} else {
std::cout << "The price entered is: " << price << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)
不过,这对我来说似乎有点挑剔。该数字必须包含一个.,其后至少有两位数字。是$可选的。
| 归档时间: |
|
| 查看次数: |
238 次 |
| 最近记录: |