this is an idea I had. remove everything above 0 like you did. then convert the decimal bit to a string. then get the length of the string.
#include <iostream>
#include <string>
#include <sstream>
int DecimalPlaces(double test)
{
double JustDecimals = test - (int) test;
std::stringstream converter;
converter<< JustDecimals;
std::string StringForm(converter.str());
return StringForm.length() - 2 /* -2 characters for the 0. */;
}
int main()
{
std::cout<< "decimal places in " << 0.235987 << " " << DecimalPlaces(0.235987);
// returns 6 which is right
}
this works but it does not test exceptions like if the input is not a decimal then dont minus the 2 otherwise DecimalPlaces(3) == -2
oh and the maximum number of decimal places seems to be 6 (with one number to the left of the decimal point).
MattyRobot
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 33
Solved Threads: 8