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

This is a discussion on How do I print out only the decimal places for a number in C? within the C Programming forums, part of the General Programming Boards category; So here is the question: Create a program that will read in a floating point number and output the decimal ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    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?

    Please explain your answer.
    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);
    Last edited by benrogers; 01-24-2011 at 01:06 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,506
    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.
    Last edited by stahta01; 01-24-2011 at 01:13 PM.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by stahta01 View Post
    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. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,289
    convert to int and subtract the int value from the float

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Elkvis View Post
    convert to int and subtract the int value from the float
    How would I do that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Elkvis View Post
    convert to int and subtract the int value from the float
    Quote Originally Posted by benrogers View Post
    How would I do that?
    Quote Originally Posted by stahta01 View Post
    floor is in math.h
    I am obligated to type something here so the post goes through.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number user input
    By jimtuv in forum C Programming
    Replies: 62
    Last Post: 06-13-2010, 11:08 AM
  2. Towr of Hanoi move the disc
    By WatchTower in forum C Programming
    Replies: 9
    Last Post: 07-17-2009, 03:48 AM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  4. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 03:31 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21