LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

get resolution of double?

Solved!
Go to solution

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 🙂

 

 

0 Kudos
Message 1 of 5
(3,593 Views)

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...

0 Kudos
Message 2 of 5
(3,581 Views)

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));

0 Kudos
Message 3 of 5
(3,578 Views)

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

0 Kudos
Message 4 of 5
(3,575 Views)
Solution
Accepted by topic author TurboMetrologist

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 5
(3,571 Views)