|
用的是boost1.46.1版本,这个版本的lexical库比1.36有改进,
float为:9 double:17;string类型的为:118 windows平台
函数集提供了精度的控制。可以按照要求输出小数点后固定格式的字符串。比如:125.000 (精确到小数点后3位)。
#include <boost/lexical_cast.hpp> #include <cmath> #include <iomanip>
using boost::lexical_cast;
bool Double2MFCStr(double dValue, CString &str, int precision) { std::string stdStr = Double2String(dValue,precision); str = StdStr2MFCStr(stdStr);
return true; }
bool MFCStr2Double(CString str, double &dValue) { std::string stdStr = MFCStr2StdStr(str); try { dValue = lexical_cast<double>(stdStr); } catch (boost::bad_lexical_cast& e) { (e); return false; } return true; }
double String2Double(const char* ch) { double nInt = 0.0; double nD = 0.0; while(*ch&&*ch!='.') { nInt = nInt*10 + *ch - '0'; ch++; }
if (*ch=='.') { ch++; nD=0.1; while(*ch) { nInt += (*ch - '0')*nD; nD = nD/10; ch++; } }
return nInt; }
bool String2Double(const std::string &stdstr, double &dValue) { try { dValue = lexical_cast<double>(stdstr); } catch (boost::bad_lexical_cast& e) { (e); return false; } return true; }
bool String2Int(const std::string &stdstr, int &nValue) { try { nValue = (int)lexical_cast<double>(stdstr); } catch (boost::bad_lexical_cast& e) { (e); return false; } return true; }
std::string Int2String(int nValue) { std::string str = ""; try { str = lexical_cast<std::string>(nValue); } catch (boost::bad_lexical_cast& e) { (e); } return str; }
std::string Int2String(int nValue,int precision) { std::string str = "";
std::stringstream ss; ss << std::fixed << std::setprecision(precision); ss << nValue;
int result; ss >> result;
try { str = lexical_cast<std::string>(result); } catch (boost::bad_lexical_cast& e) { (e); } return str; }
double Round(double dValue,int precision) {
double minus = dValue>0?1.0:-1.0; for (int i=0;i!=precision+1;i++) { minus = minus/10.0; } if ((fabs(dValue)-fabs(minus))<1e-6) { return 0; } double m = minus*5; std::stringstream ss; ss << std::fixed << std::setprecision(precision); ss << dValue ;
double result; ss >> result; return result + m; }
std::string Double2String(double dValue,int precision) { std::string str = ""; try { str = lexical_cast<std::string>(Round(dValue,precision)); size_t n = str.find('.'); if ( n == std::string::npos) { str.append("."); for (int i=0;i!=precision;i++) { str.append("0"); } }else { if ((str.size()> n+precision)) //后面至少还有decplaces位吗? { str[n+precision+1]='\0';//覆盖第一个多余的数 str.swap(std::string(str.c_str()));//删除nul之后的多余字符 }else { size_t size=str.size(); for (int i=0;i!=(n+precision+1-size);i++) { str.append("0"); } } } } catch (boost::bad_lexical_cast& e) { (e); } return str; }
std::string MFCStr2StdStr(CString str) { std::string strStl; #ifdef _UNICODE CStringA stra(str.GetBuffer(0)); str.ReleaseBuffer(); strStl = stra.GetBuffer(0); stra.ReleaseBuffer(); #else strStl=str.GetBuffer(0); #endif return strStl; }
CString StdStr2MFCStr(const std::string &stdstr) { //CString strMFC; //strMFC = stdstr.c_str(); //return strMFC; if (stdstr=="") { return CString(); }else { return CString(stdstr.c_str()); } }
void Split(const CString& source,const CString& division,CStringArray& dest) { //CStringArray dest; dest.RemoveAll(); int pos = 0; int pre_pos = 0; while( -1 != pos ) { pre_pos = pos; pos = source.Find(division,(pos+1)); dest.Add(source.Mid(pre_pos,(pos-pre_pos))); } }
char* MFCString2Char(CString str) { char * szText = new char[str.GetLength()+1] ; WideCharToMultiByte(CP_ACP,0,str.GetBuffer(),-1,szText,MAX_PATH,NULL,NULL); return szText; }
errno_t Char2WChar_t(const char *pChar,wchar_t* pWchar) { if (pWchar==NULL) { return EINVAL; } // Convert to a wchar_t* size_t origsize = strlen(pChar) + 1; const size_t newsize = 1024; size_t convertedChars = 0; pWchar = new wchar_t[newsize]; return mbstowcs_s(&convertedChars, pWchar, origsize, pChar, _TRUNCATE); }
void GetFileTitleFromFileName(CString fullPath, CString& dirName, CString& fileNameWithOutExt, CString& ExtendName) //获取文件名和扩展名 { int Where; Where=fullPath.ReverseFind('\\'); if(Where==-1) { Where=fullPath.ReverseFind('/'); } dirName = fullPath.Left(Where); fileNameWithOutExt = fullPath.Right(fullPath.GetLength()-1-Where);
int Which=fileNameWithOutExt.ReverseFind('.'); ExtendName=fileNameWithOutExt.Right(fileNameWithOutExt.GetLength()-Which-1); if (Which!=-1) { fileNameWithOutExt=fileNameWithOutExt.Left(Which); } }
/**//* string to time_t 时间格式 2009-3-24 */ int StringToTime(const std::string &strDateStr,time_t &timeData) { char *pBeginPos = (char*) strDateStr.c_str(); char *pPos = strstr(pBeginPos,"-"); if(pPos == NULL) { return -1; } int iYear = atoi(pBeginPos); int iMonth = atoi(pPos + 1); pPos = strstr(pPos + 1,"-"); if(pPos == NULL) { return -1; } int iDay = atoi(pPos + 1); struct tm sourcedate; memset((void*)&sourcedate,0,sizeof(sourcedate)); sourcedate.tm_mday = iDay; sourcedate.tm_mon = iMonth - 1; sourcedate.tm_year = iYear - 1900; timeData = mktime(&sourcedate); return 0; } /**//* time_t to string */ int TimeToString(std::string &strDateStr,const time_t &timeData) { char chTmp[15]; memset(chTmp,0,sizeof(chTmp)); struct tm *p=NULL; localtime_s(p,&timeData); p->tm_year = p->tm_year + 1900; p->tm_mon = p->tm_mon + 1; sprintf_s(chTmp,sizeof(chTmp),"04d-%02d-%02d", p->tm_year, p->tm_mon, p->tm_mday); strDateStr = chTmp; return 0; }
|