温馨提示×

c++怎么保留两位小数输出

c++
小亿
367
2024-05-31 16:32:11
栏目: 编程语言

可以使用iomanip库中的setprecision函数来控制浮点数输出的小数位数。下面是一个示例代码:

#include <iostream>
#include <iomanip>

int main() {
    double number = 3.14159;
    
    std::cout << "Number with two decimal places: " << std::fixed << std::setprecision(2) << number << std::endl;
    
    return 0;
}

在上面的代码中,我们使用std::fixed来指定小数点后的位数固定,并使用std::setprecision(2)来设置输出的小数位数为两位。

0