Any who can help!!
let i have a number 16.0230045
i want to count the fractional part of this number means i want to get the number of digits of the fractional part of this number and store each digit in an array as well.
i.e i have a[size];//where size is the number of digits of the fractional parts of my number how can i do that using C++.
finally i should have my array to hold all the digits.
Remember my number can be type float ,or double.
Careful. Remember that there's floating point error, and you most likely do not have precisely the floating point number 16.0230045. More likely, you have something like
16.02300450000018945
16.023004500328585832
16.02300450039596595
etc. You'll need to choose a cutoff precision for this problem.
Once you decide upon that, I recommend checking out the modf function, which separates a double into its integer and fractional parts. After that, you could use the sprintf() function to put it into a string array.
Alternatively, you could use sprintf() to put the entire real/float/double into a character string, start looking for the decimal point in a way similar to that posted above. Note that regardless, you'll need to choose a precision. e.g.,
-
double some_number = 16.0230045;
-
char str [1024];
-
sprintf( str, "%3.8f", some_number );
-
// count digits preceding and following the decimal point.
-
// remove the trailing zeros, if decided.
-
// etc.
-
I hope that's helpful. Floating point error can be tricky, but you'll get the hang of it very, very quickly. -- Paul