格式化高精度浮点数

来源:互联网 发布:aspen plus软件 编辑:程序博客网 时间:2024/05/01 05:59
#include <math.h>#ifndef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES #define _stprintf_s _stprintf#define _tcscat_s _tcscat#endif //#ifndef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES LPCTSTR Double2Str(long double fVal){  static TCHAR chStr[256];  memset(chStr, 0, sizeof(chStr));    _stprintf_s(chStr, _T("%+I64d"), (__int64)fVal);  long double fRem = fabs(fVal - (__int64)fVal);  _tcscat_s(chStr, _T("."));  do  {    fRem *= 10.0;    TCHAR chSub[] = { _T('0' + (int)fRem), _T('\0') };    _tcscat_s(chStr, chSub);        fRem = fRem - (int)fRem;  }while(fRem != 0);  TRACE(_T("fVal = %f == >%s\n"), fVal, chStr);  return chStr;}


//测试  double fVal = 4 * atan(1.0);  Double2Str(fVal);

/*输出fVal = 3.141593 == >+3.141592653589793115997963468544185161590576171875当然8字节的双精度数据有效位为16位,之后的也就没有实际意义了*/


原创粉丝点击