现在的位置: 首页 > 综合 > 正文

格式化高精度浮点数

2013年10月11日 ⁄ 综合 ⁄ 共 678字 ⁄ 字号 评论关闭
#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位,之后的也就没有实际意义了
*/

抱歉!评论已关闭.