格式化输出
本课属于 Coddy C++ 学习旅程的“基础知识”单元。第 41 课,共 74 课。
挑战
入门修改输出,使其始终以两位小数的形式打印 double 值。为此,请将 << 运算符与 std::fixed 和 std::setprecision(2) 一起使用:
#include <iomanip>
std::cout << std::fixed << std::setprecision(2) << num;你需要包含 <iomanip>
亲手试试
#include <iostream>
int main() {
std::cout << "Calculator App" << std::endl;
double num1;
std::cin >> num1;
double num2;
std::cin >> num2;
// Perform arithmetic operations
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double division = num1 / num2;
// Print the results
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Division: " << division << std::endl;
return 0;
}基础知识中的所有课
自己练习: C++ 在线编译器