ava*_*123 5 c double scientific-notation
一个简单的问题,但我无法获得有关这种格式的文档:我想以Fortran科学记数法打印浮点数,其整数部分始终为零.
printf("%0.5E",data); // Gives 2.74600E+02
Run Code Online (Sandbox Code Playgroud)
我想像这样打印:
.27460E+03
Run Code Online (Sandbox Code Playgroud)
如何让这个结果尽可能干净?
我尝试使用log10()和执行此操作pow(),但最终遇到了舍入错误的问题。因此,正如@Karoly Horvath所评论的,字符串操作可能是最好的方法。
#include <stdlib.h>\n\nchar *fortran_sprintf_double(double x, int ndigits) {\n char format[30], *p;\n static char output[30];\n\n /* Create format string (constrain number of digits to range 1\xe2\x80\x9315) */\n if (ndigits > 15) ndigits = 15;\n if (ndigits < 1) ndigits = 1;\n sprintf(format, "%%#.%dE", ndigits-1);\n\n /* Convert number to exponential format (multiply by 10) */\n sprintf(output, format, x * 10.0);\n\n /* Move the decimal point one place to the left (divide by 10) */\n for (p=output+1; *p; p++) {\n if (*p==\'.\') {\n *p = p[-1];\n p[-1] = \'.\';\n break;\n }\n }\n\n return output;\n}\nRun Code Online (Sandbox Code Playgroud)\n