|
发表于 2016-12-9 09:52:33
|
显示全部楼层
atof 精度问题
经常要用到将浮点字符串转为浮点数,之前一直用的是atof,这个是返回float ,即有效数字是在小数点后六位,如果对精度要求更高的话就需要用 strtod 这个函数了 。
并且这个可以处理更复杂的情况:
- int main ()
- {
- char szOrbits[] = "365.24 29.53";
- char * pEnd;
- double d1, d2;
- d1 = strtod (szOrbits,&pEnd); //自动处理截断,并将后一部分赋值到pend,如果不需要则用NULL
- d2 = strtod (pEnd,NULL);
- printf ("The moon completes %.2f orbits per Earth year.\n", d1/d2);
- return 0;
- }
复制代码
那直接实现 atof 的功能:
char *str = "113.29464653";
double d = strtod(str,NULL);
参考:
1:http://www.cplusplus.com/reference/clibrary/cstdlib/strtod/ |
|