C++ 在数字开头添加小数点的任意方法

C++ Any way to add a decimal point to the start of a number?

本文关键字:任意 方法 小数点 添加 数字 开头 C++      更新时间:2023-10-16

因此,我应该制作一个程序来询问批发商品的价值,该商品被标记的百分比,并使用函数计算和显示零售价格。问题是我明确应该提示输入整数,所以如果说标记是 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;