# Thread: I can print digits with putchar but how to print floats with put char?

1. ## I can print digits with putchar but how to print floats with put char?

Hello

So the function below can print any int using putchar. But I can't figure out how to print floats using putchar. Any ideas on this?

Regards!

Code:
```void putDigit(int n){
int pow = 1;
char lastChar = ' ';
if (n < 0){
putchar('-');
lastChar = (char)((int)'0' + (n % 10));
n /= 10;
n *= (-1);
}
while (pow * 10 <= n)
pow *= 10;
while (pow != 0){
int d = n / pow;
putchar((char)((int)'0' + d));
n = n - d * pow;
pow /= 10;
}
if (lastChar != ' '){
putchar(lastChar);
}
}``` 2. Dear Friend,

You can't get the floating value correctly. Because in the putDigit function you gave the argument type as int. So if we pass the floating point value , it will convert the float into int. So it will take the number before "." only.

If we change the argument type as float also, it will give the error. Because we can't perform the % operation for the floating point value.

Thank you, 3. ## floating value is not possible

you can print the integer values using your function. you cannot print the floating values.
because.

[%] modules operations are only allowed for integer. see in your code the following line
Code:
`lastChar = (char)((int)'0' + (n % 10));`
The negative number's is not possible your code.
================================================== ========================
you can handling the '.' dot in your code.

see the following code in this code I am handling [.] floating values
Code:
```double atof(char s[])
{
double val, power;
int i, sign;

for(i=0;isspace(s[i]); i++)// skip white space
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;

for (val =0.0; isdigit(s[i]);i++)
val = 10.0 * val + (s[i]-'0');

if (s[i] == '.')
i++;

for(power = 1.0; isdigit(s[i]); i++)
{
val = 10.0 * val + (s[i]-'0');
power *=10.0;
//printf("val:%d",val);
}
return sign * val/ power;
}``` 4. Here's one way (not necessarily the best, but very intuitive), which I will describe to you in an example and let you do the coding.
Say our number is 6543.21
I take the log10 of the number rounded down, which is 3.
I divide the number by 10^3 and round down, which is 6.
I print out the 6.
subtract 6x10^3 from the number. Now the number is 543.21
I take the log of the number rounded down: 2
and so on.
keep doing this until you have printed out the number of digits that you want. 5. Originally Posted by murugaperumal Dear Friend,

You can't get the floating value correctly. Because in the putDigit function you gave the argument type as int. So if we pass the floating point value , it will convert the float into int. So it will take the number before "." only.

If we change the argument type as float also, it will give the error. Because we can't perform the % operation for the floating point value.

Thank you,
Thanks so much for clearing that up, Murugaperumal. The first step is to study and understand the floating-point specs for your particular machine (typically IEEE 754). Short of that, you can always fall back on ssprintf or similar, of course, tho I'm assuming that the purpose of this exercise is to do everything from scratch, correct? 6. Originally Posted by NeonBlack Here's one way (not necessarily the best, but very intuitive), which I will describe to you in an example and let you do the coding.
Say our number is 6543.21
I take the log10 of the number rounded down, which is 3.
I divide the number by 10^3 and round down, which is 6.
I print out the 6.
subtract 6x10^3 from the number. Now the number is 543.21
I take the log of the number rounded down: 2
and so on.
keep doing this until you have printed out the number of digits that you want.
what do you mean when you say "I divide the number by 10^3 and round down, which is 6." you divide 3 by 1000 ? 7. 6543.21/1000 = 6.54321 8. I'm having some trouble making this work. Any one want to give this a try? 9. Code:
```void putcf( float f )
{
char buf[ BUFSIZ ] = {0};
size_t x;

for( x = 0, sprintf( buf, "%f", f ); x < strlen( buf ); x++ )
putchar( buf[ x ] );
}```

Quzah. 10. Originally Posted by NeonBlack 6543.21/1000 = 6.54321
Here is what I coded it and it prints everything upto the decimal point.

Code:
```void putFloat(double n){
double x;
int r, second;
x = log10(n);
r = n/1000;
putDigit(r);
second = n - (6*1000);
putDigit(second);
double x;
int r, second;
x = log10(n);
r = n/1000;
putDigit(r);
second = n - (6*1000);
putDigit(second);

}```
But how can I print the dot and everything after that now? 11. No offense, but you've really taken a wrong turn on this, imo.

Take what you have up above this post, and toss it - and the farther the the better. Look carefully at what Quzah has posted above. Despite the "little surprise" he's placed in it, that is how it should be done.

It's clear, direct, concise.

What more could you ask for? Popular pages Recent additions 