gpt4 book ai didi

c++ - 小数输出后加两个零

转载 作者:行者123 更新时间:2023-12-05 03:37:14 24 4
gpt4 key购买 nike

我已经查过了,但我还没有得到任何工作,但基本上我需要这段代码:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
ifstream inFile;
ofstream outFile;

string firstName, lastName, department;
double salary, bonus, taxes;
double distance, timeTravel;
int coffeeCups;
double cupCost;
double paycheck, aveSpeed, salesAmount;

inFile.open("inData.txt");
outFile.open("outData.txt");

inFile >> firstName >> lastName >> department;
inFile >> salary >> bonus >> taxes;
inFile >> distance >> timeTravel;
inFile >> coffeeCups >> cupCost;

paycheck = salary + salary*(bonus/100) - ((salary+ salary*(bonus/100))*(taxes/100));
aveSpeed = distance/timeTravel;
salesAmount = coffeeCups * cupCost;

outFile << "Name: " << firstName << " " << lastName << ", Department: " << department << endl;

outFile << "Monthly gross salary: $" << salary << ", Monthly bonus: " << bonus << "%" << ", Taxes: " << taxes << "%" << endl;

outFile << "Paycheck: $" << paycheck << endl << endl;

outFile << "Distance traveled: " << distance << " miles, Traveling time: " << timeTravel << " hours" << endl;

outFile << "Average speed: " << aveSpeed << " miles per hour" << endl << endl;

outFile << "Number of coffee cups sold: " << coffeeCups << ", Cost: $" << cupCost << " per cup" << endl;

outFile << "Sales amount = $" << salesAmount;

inFile.close();
outFile.close();

return 0;
}

输出这个:

Name: Giselle Robinson, Department: Accounting
Monthly Gross Salary: $5600.00, Monthly Bonus: 5.00%, Taxes: 30.00%
Paycheck: $4116.00

Distance Traveled: 450.00 miles, Traveling Time: 9.00 hours
Average Speed: 50.00 miles per hour

Number of Coffee Cups Sold: 75, Cost: $1.50 per cup
Sales Amount = $112.50

但是,我得到的是:

Name: Giselle Robinson, Department: Accounting
Monthly gross salary: $5600, Monthly bonus: 5%, Taxes: 30%
Paycheck: $4116

Distance traveled: 450 miles, Traveling time: 9 hours
Average speed: 50 miles per hour

Number of coffee cups sold: 75, Cost: $1.5 per cup
Sales amount = $112.5

最佳答案

// Add this before you start writing to the file
outFile << std::fixed << std::setprecision(2) << std::setfill('0');

// Your original output code below
outFile << "Name: " << firstName << " " << lastName << ", Department: " << department << endl;
...

信用https://stackoverflow.com/a/15327758/10808581

关于c++ - 小数输出后加两个零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69382756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com