07-11-2011 08:53 AM
When converting a string to double the result prints out as 0.000000. Of course this is because the default resolution is to six digits. Using 10 digits of resolution gives 0.0000000247. What is the function or the best way to detect the length of this double and print it? (I wish the ".10f" could use a variable in place of 10)
example:
char data[]={"+2.47600E-08"};
double result;
result=atof(data);
DebugPrintf("%f\n",result);
DebugPrintf("%.10f\n",result);
Thanks, and sorry for the newbie question 🙂
Solved! Go to Solution.
07-11-2011 09:27 AM
What do you mean by length of a double?
The number of zeros after the decimal point can be calculated using the logarithm of the number...
07-11-2011 09:46 AM
I should split this into two questions..
1.
Yes, that is correct. The log of the double would return the amount of decimal places, however if using something like
DebugPrintf("%f\n",log(atof(dataBuffer)));
then a range error occurs (and the result of the log would have to be rounded up or down to an integer if the error doesnt happen). After getting this number then my next question would be the following..
2.
Am I able to use a calculated number in place of [here], instead of a static number?
DebugPrintf(%[here]f\n",atof(dataBuffer));
07-11-2011 09:55 AM
1) I would not use a compound expression as in your example, because atof may fail. Also, for negative numbers you should consider using the absolute value
2) To my knowledge not as simple as you wrote, but in a first statement you could build your format string "here" using sprintf and then use it in a second formatting function
07-11-2011 10:13 AM - edited 07-11-2011 10:13 AM
As per your question #2, you cannot do this with DebugPrintf, but using Fmt from the Formatting and I/O Library you can have a parametric number of decimal places in the string representation of a number: see Using Asterisks (*) Instead of Constants in Format Specifiers argument in this help topic.