C++ 在数字开头添加小数点的任意方法
C++ Any way to add a decimal point to the start of a number?
因此,我应该制作一个程序来询问批发商品的价值,该商品被标记的百分比,并使用函数计算和显示零售价格。问题是我明确应该提示输入整数,所以如果说标记是 50%,用户应该输入"50"。有没有办法在 50 的前面添加一个小数点来简化?
为了清楚起见,我将包括我的代码。
int main() {
double cost;
double markup;
double total;
cout << "Enter the item's wholesale cost: ";
cin >> cost;
cout << "nEnter the item's markup percentage: ";
cin >> markup;
cout << endl;
total = calculateRetail(cost, markup);
cout << fixed << showpoint << setprecision(2);
cout << "The item's retail price is $" << total << endl;
return 0;
}
double calculateRetail(double cost, double markup)
{
//This is where the code to convert "markup" to a decimal would go
cost += markup * cost;
return cost;
}
"
将小数点向左移动两位"与"将数字除以 100"的操作相同。
markup /= 100;
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 在 std::vector<无符号字符中存储任意数据的方法>
- std::is_invocable 用于测试任意方法是否存在的语法(不仅是运算符())
- 从任意容器中廉价删除元素的惯用方法?
- 如何根据特定类型任意启用或禁用类方法
- 包含一批库和目录路径以"Include Additional Directories"视觉C++的任意方法
- 接受任意数量的回调并存储结果的 C++ 类方法
- C++ 在数字开头添加小数点的任意方法
- 递归类型真的是构建不连续的任意大小数据结构的唯一方法吗
- 指向任意类方法的模板非类型指针
- 使用 Google C++ 模拟框架 (Google Mock) (V1.5) 将任意参数传递给调用的方法
- 这是从连续的单词序列中提取任意范围的位的最有效方法
- 行号:使用 __LINE__ 返回输入文件的行号的任意方法
- 实现任意类型擦除的小缓冲区优化的简单方法(如std::function.)
- c++中向std::vector中添加任意数量元素的最有效方法是什么?
- 在c++中向类方法传递任意的lamba
- 使函数接受其参数列表的任意排列的规范方法是什么?
- 如何在同一列表中存储指向任意类类型的方法指针
- 如何在c++11中存储任意方法指针