Thread: Printing out only the decimal places for a number in C?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    1

    Printing out only the decimal places for a number in C?

    Hello, I am trying to create a program that will read a double precision floating point number and output only the decimal part of the number given. For example, Input: 2.39 Output: 0.39
    I am struggling to overcome this hurdle. So far I can only make it output 0.00.

    Code:
    int main (void){
        double amount = 0.0;
        int Loonies = 0;
    
    
        printf("Please enter the amount to be paid: $"); 
        scanf("%if",&amount);
        
        printf("Loonies required: %i", amount);
        printf(", balance owing $%0.2f", amount);
    
    
            return 0;
    }
    Any help or suggestions would be greatly appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable number of decimal places in float
    By blixel in forum C Programming
    Replies: 2
    Last Post: 02-11-2018, 06:43 PM
  2. User-Defined Number of Decimal Places?
    By gucci_mane_BURR in forum C Programming
    Replies: 2
    Last Post: 09-02-2013, 06:40 PM
  3. Replies: 6
    Last Post: 01-24-2011, 03:01 PM
  4. Printing specified number of decimal places?
    By 843 in forum C Programming
    Replies: 4
    Last Post: 11-26-2010, 03:47 AM
  5. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM

Tags for this Thread