
C++
iomanip库中的setprecision函数。下面是一个简单的例子,演示如何保留小数点后两位小数:<code>cpp<br/>#include <IOStream> #include <iomanip> // 包含iomanip库 int mAIn() double number = 3.14159265358979323846; // 定义一个double类型的数 std::cout << std::fixed << std::setprecision(2) << number << std::endl; // 使用std::fixed固定小数点后的位数,std::setprecision(2)设置为保留两位小数 return 0; }</code>在这个例子中,
std::fixed确保了数字会以固定的小数点位数输出,而std::setprecision(2)则指定了我们要保留两位小数。你可以根据需要将2替换为其他数字来保留不同数量的小数位。希望这能帮到你!Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号