*sigh* I didn't want to bother posting this because I'll probably get flamed, but maybe you can make use of it--
/**
* Numerics.h
*/
#ifndef NUMERICS
#define NUMERICS
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
/**
* Convenience class for enabling easy
* conversions between strings and primitive types.
*
* This class is not meant to support non-primitive types,
* but it should if target class Type has istream and ostream
* operators overloaded.
*/
namespace Numerics{
template<class T> class Numerical;
template<>
class Numerical<float>{
private:
float number;
int precision;
static const int PRECISION_LIMIT = 60;
public:
/**
* Constructs a Numerical<float> based on a float
*/
Numerical(float value = 0) : number(value), precision(8){
(*this)(getString());
}
/**
* Attempts to construct a Numerical<float> based on the string input
*/
Numerical(const string& arg){
(*this)(arg);
}
/**
* Sets the precision setting. The setting will be adjusted to fit within 1-to-PRECISION_LIMIT
* which is 60.
*/
Numerical<float>& setPrecision(int pv){
precision = (pv >= 1) ? ( (pv <= PRECISION_LIMIT) ? pv : PRECISION_LIMIT) : 1;
(*this)( getString() );
return *this;
}
/**
* Returns the current precision setting
*/
int getCurrentPrecision(){return precision;}
/**
* Attempts to assign the argument value to the value
* of this Object's type.
* If the value is invalid, nothing happens.
*/
string operator()(const string& arg){
stringstream ss (stringstream::in | stringstream::out);
try{
ss << setprecision(precision);
ss << arg;
ss >> number;
}catch(...){
throw string("Invalid String!");
}
return ss.str();
}
/**
* Attempts to …