(7条消息) c++ 的string的格式化方法

  1. string strResult,str;
  2. double a=35.000,b=14.967;
  3. std::ostringstream ostr;
  4. ostr << "a+b=" << a << "+" << b << "=" << a+b;
  5. std::string str = ostr.str();
  6. strResult += str;

2.

  1. #include <sstream>
  2. #include <iostream>
  3. using namespace std;
  4. ostringstream ostr;
  5. ostr << "d = " << 123 << "f = " << 12.345 << "test format" << std:endl;
  6. string str = ostr.str();
  7. cout << ostr.str().c_str();

以上两种方法中,double数值也默认显示为整数。

3.

  1. char buf[1024];
  2. sprintf(buf,"a+b=%f+%f=%f",a,b,a+b);
  3. memset(buf,0,sizeof(buf));
(0)

相关推荐