默认情况下,用科学计数法表示实数,输出的指数位数为3。如何控制使其只输出2位指数位?VC6下如何?VC2005又如何?
在VC2005下,运行库提供一个函数_set_output_format可以控制printf输出的实数的指数位,要输
出2位指数位需要这样设置:
unsigned int old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
使用完恢复原来设置:
_set_output_format(old_exponent_format);
在VC6中没有此函数,相同功能的函数我也没有发现。
在C++标准库中的stream中,我也没有找到这样的格式控制符。为了在VC6下完成此功能,最后只能
选择曲线救国的道路了——删除一个0。
1、对于printf这样的输出解决方法:
if(pos1 == string::npos)
return;
pos1 +=2;
str.erase(pos1, 1);
EraseZeroPlus(str, pos1);
}
void EraseZeroMinus(std::string& str, string::size_type pos)
{
string::size_type pos1= str.find("E-", pos);
if(pos1 == string::npos)
return;
pos1 +=2;
str.erase(pos1, 1);
EraseZeroMinus(str, pos1);
}
void EraseZero(char* szBuf, FILE* pFile)
{
string str(szBuf);
EraseZeroPlus(str, 0);
EraseZeroMinus(str, 0);
fputs(str.c_str(), pFile);
}
2、对于stream这样的输出解决方法:
这里只是考虑输出文件的情况。
.h file
typedef my_ofstream<char> myofstream;
// overload operator for double and float value
myofstream& operator<< (myofstream& strm, float value);
myofstream& operator<< (myofstream& strm, double value);
.cpp file:
ostringstream oss;
oss << scientific << showpoint << setprecision(strm.get_precision()) << value;
std::string str(oss.str());
erase_one_zero(str);
strm << str;
return strm;
}
myofstream& operator<< (myofstream& strm, const double value)
{
using namespace std;
ostringstream oss;
oss << scientific << showpoint << setprecision(strm.get_precision()) << value;
std::string str(oss.str());
erase_one_zero(str);
strm << str;
return strm;
}
test code:
myofstream fout("out.txt");
fout << "out put scientific format: /n";
fout << 3654.002 << endl;
fout << 0.145f << endl;
fout.set_precision(6);
fout << 3654.002 << endl;
fout << 0.145f << endl;
return 0;
}
VC6下,不知还有没有更好的办法……