Go to the documentation of this file.00001 #ifndef ROUND_STRING_H
00002 #define ROUND_STRING_H
00003
00004 #include <iomanip>
00005 #include <sstream>
00006 #include <string>
00007 #include <sstream>
00008 #include <cmath>
00009
00010 struct round_string {
00011 template<class T>
00012 std::string operator()(const std::pair<T,T> x) const {
00013 int val_digit(0), err_digit(0);
00014
00015 if(x.first != 0) {
00016 while( fabs(x.first) / pow(10, val_digit) < 1 ) val_digit--;
00017 while( fabs(x.first) / pow(10, val_digit) > 10 )val_digit++;
00018 }
00019 if(x.second != 0 ) {
00020 while( x.second / pow(10,err_digit) < 0.95 ) err_digit--;
00021 while( x.second / pow(10,err_digit) > 9.50 ) err_digit++;
00022 }
00023
00024 if(val_digit<err_digit) val_digit=err_digit;
00025 const bool scinot = (val_digit<-1 || err_digit>0);
00026
00027 std::stringstream s;
00028 s << std::fixed << std::setprecision( scinot? val_digit-err_digit : -err_digit)
00029 << ( scinot? x.first/pow(10,val_digit) : x.first )
00030 << "("
00031 << unsigned(x.second / pow(10,err_digit) + 0.5) << ")";
00032 if(scinot) s<< "e" << (val_digit>0 ? "+" : "") << val_digit;
00033
00034 return s.str();
00035 }
00036 };
00037
00038 #endif