C Primer Plus书中代码注释-chapter17_7_showpt.cpp

chapter17_7_showpt.cpp

#include <iostream>

int main(){
	using std::cout;
	using std::ios_base;
	
	float price1 = 20.40;
	float price2 = 1.9 + 8.0 / 9.0;
	
	cout.setf(ios_base::showpoint);								//保留6位有效数字,这是float类型的默认精度
	cout << "\"Furry Friends\" is $" << price1 << "!\n";
	cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
	
	cout.precision(2);
	cout << "\"Furry Friends\" is $" << price1 << "!\n";		//设置精度为2位,在showpoint和precision的共同作用下,保留带有小数的十位数时,保留了小数点
																//即使这个数值是整数,也打印小数点
	cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiaoyami/article/details/107671203
今日推荐