Basically i tried to output a double variable with but only PrintDigit(char ). Here is my code
void printDigit(const char c)
{
cout << c ;
}
void PrintDigits(double target)
{
if (target < 0)
{
printDigit('-');
target = -target ;
PrintDigits((int)target / 10);
}
else if ((int)target / 10 == 0)
{
printDigit('0' + (int)target % 10);
if (target - (int)target % 10 != 0)
{
target -= (int)target % 10;
do {
target *= 10 ;
//for debugging
cout << endl << target << endl;
cout << floor(target)<<" "<<ceil(target)<<endl;
} while (floor(target) != ceil(target));
printDigit('.');
PrintDigits(target);
}
}
}
And there are some strange problems ,here is the output when i tried to input 3.53: check the output when target==53!
My question is why floor(target)==52 while target==53 ? And is there any better way to do this ?(with only PrintDigit(char) function and don't transfer the double variable to string)