Alright here is how I would go about the approach:
First print out the integer part:
double value = 123.456
char buffer[50];
int bufferSize = 0;
int intPart = (int)value;
//find the size of the intPart by repeatedly dividing by 10
int tmp = intPart, size = 0;
while(tmp){
size++;
tmp /= 10;
}
//print out the numbers in reverse
for(int i = size-1; i >= 0; i--){
buffer[i] = (intPart % 10) + '0';
intPart /= 10;
}
bufferSize += size;
Now that you have printed out the integer part, its time to print out the decimal part. The way I would approach it would be to multiply it by ten until it is an integer.
value -= intPart;
while(floor(value) != value)
value *= 10;
And now I would repeat the same process above:
intPart = (int)value;
//find the size of the intPart by repeatedly dividing by 10
tmp = intPart, size = 0;
while(tmp){
size++;
tmp /= 10;
}
//print out the numbers in reverse
for(int i = size-1; i >= 0; i--){
buffer[bufferSize + i] = (intPart % 10) + '0';
intPart /= 10;
}
bufferSize += size;
And finally the finishing touch:
buffer[bufferSize] = 0;