# Thread: How do I print out only the decimal places for a number in C?

1. ## How do I print out only the decimal places for a number in C?

So here is the question:

Create a program that will read in a floating point number and output the decimal part of the number. e.g. given 3.14159, it will output .14159

I do not know the %f for only showing the decimal part of the number the user enters. What is it to only show the decimal places of a number?

Code:
```int main()
{
float n1;

printf("Please enter a number with 4 decimal places:\n");
scanf("%f", &n1);
printf("The decimal places you entered are %f .\n", n1);```

2. floor is in math.h
Code:
`printf("The decimal places you entered are %f .\n", n1-floor(n1));`
%f may need to be %.4f

Tim S.

3. Originally Posted by stahta01
floor is in math.h
Code:
`printf("The decimal places you entered are %f .\n", n1-floor(n1));`
%f may need to be %.4f

Tim S.
I've tried that and it still prints out the whole number in front of the decimal places. Is there anyway to - the portion of the whole number so I am just left with the decimals?

4. convert to int and subtract the int value from the float

5. Originally Posted by Elkvis
convert to int and subtract the int value from the float
How would I do that?

6. Originally Posted by Elkvis
convert to int and subtract the int value from the float
Originally Posted by benrogers
How would I do that?
Originally Posted by stahta01
floor is in math.h
I am obligated to type something here so the post goes through.

7. Thanks a bunch.

Code:
```#include <stdio.h>
#include <math.h>

int main()
{
float n1;

printf("Please enter a number with at least 4 decimal places:\n");
scanf("%f", &n1);
printf("The decimal places you entered are %f .\n", n1-floor(n1));

return 0;
}```
works perfectly!