How to get a value Before/After a decimal point in C ??

This is a discussion on How to get a value Before/After a decimal point in C ?? within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to C, I would like to achieve the following. get two numbers after the decimal point 23.456 ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    Smile How to get a value Before/After a decimal point in C ??

    Hi,

    I'm new to C, I would like to achieve the following.

    get two numbers after the decimal point

    23.456 -> 0.45
    11.235224 -> 0.23

    and get all the numbers before the decimal point

    23.456 -> 23
    11.23 -> 11
    .42 -> 0

    How can I do it? I know it might be easy to some, but for me I still don't feel comforatble with C.
    Last edited by ammar555; 05-18-2011 at 12:55 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    23.456 is a floating point number

    23.456 -> 23
    Thats simply an integer version of your floating point number.

    .456 is what's left if you subtract your integer from your float. You might have to play around with your code to get it to work right if you don't fully understand how c handles integers and floats.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Here is my code, but it doesn't quit work.

    Code:
     int main()
    
    {
    
     float cost; //Define the cost as float
     float paid; //Define payments as float
     float sum;
    
     printf("Total Cost: ");
     scanf ("%f", &cost);
     printf("Paid: ");
     scanf("%f", &paid ) ;
     sum = paid - cost; //Subtract payments from the cost
     
     printf ("\n%.d dollars", sum); 
     
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    You can use the trunc function in math.h to get the integer portion, then subtract that from the float to get the decimal portion. Depending on your setup, you may need to link the math library separately.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Code:
     int main()
     float sum;
    ...
     printf ("\n%.d dollars", sum);
    Read up on how to use printf. You don't get to mix and match format specifies all willy-nilly. %d is for printing an integer (meaning sum is of type int, not "I want to drop the fractional part").

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    5
    I'm trying to make a program that calculates the change , like below.

    Total Cost: 2.23
    Paid: 3.00

    Results: $.77

    3 quarters
    0 dimes
    0 nickels
    2 pennies

    Let's say now we entered the following .

    Total Cost: 3.25
    Paid: 10.00

    Result: $6.75

    27 quarters (I don't want it all to be in quarters, I need dollars too.)
    0 dimes
    0 nickels
    0 pennies

    so I was trying to take the value before the decimal point and count it as dollars and what's after I will count it in quarters, dimes or whatever as needed.
    Last edited by ammar555; 05-18-2011 at 01:22 PM.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    5
    Here is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
     const int QUARTER = 25;
     const int DIME = 10;
     const int NICKEL = 5;
     const int DOLLAR = 100;
    
    int main()
    {
    
     float cost; //Define the cost as float
     float paid; //Define payments as float
     char ch;
     float sum;
     int sum2;
     int amount, dollars, quarters, dimes, nickels, pennies;
    
     printf("Total Cost: ");
     scanf ("%f", &cost);
     printf("Paid: ");
     scanf("%f", &paid ) ;
     sum = paid - cost; //Subtract payments from the cost
    
     //printf ("\n%.f dollars", sum);
     printf ("\n\n-------------");
     printf ("\n%.2f Result", sum);
     printf ("\n-------------");
    
     amount = sum * 100;
    
     quarters = amount / QUARTER;
     amount = amount % QUARTER;
     dimes = amount / DIME;
     amount = amount % DIME;
     nickels = amount / NICKEL;
     pennies = amount % NICKEL ;
    
    printf("\n\n%d quarters\n%d dimes\n%d nickels\n%d pennies\n",
    quarters, dimes, nickels, pennies);
    
    
     printf("\n\nContinue? (y/n)");
        while(kbhit()) getch(); /* clear the input buffer */
        do ch = getch(); while(ch!='y' && ch!='n');
        while(ch=='y');
        return 0;
    
    }

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by mike65535 View Post
    23.456 is a floating point number



    Thats simply an integer version of your floating point number.

    .456 is what's left if you subtract your integer from your float. You might have to play around with your code to get it to work right if you don't fully understand how c handles integers and floats.

    I found it thanks, here is my code for those who will need it in the future.

    Code:
    int main()
    
    {
    
     float cost; //Define the cost as float
     float paid; //Define payments as float
     int sum;
     float sum2;
    
     printf("Total Cost: ");
     scanf ("%f", &cost);
     printf("Paid: ");
     scanf("%f", &paid ) ;
     sum = paid - cost; //Subtract payments from the cost
     printf ("\n%d dollars", sum);
     sum2 = (paid - cost ) - sum;
      printf ("\n%.2f Remainder", sum2);
     //sum2 = sum - (paid - cost);
     //printf ("\n%d dollars", sum);
    
    return (0);
    }
    Last edited by ammar555; 05-18-2011 at 01:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to remove zero after decimal point
    By abhay_m8 in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2007, 12:11 AM
  2. Changing decimal point
    By Ikon in forum C Programming
    Replies: 7
    Last Post: 03-01-2006, 05:36 PM
  3. decimal point allignment
    By Max in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 10:27 PM
  4. Testing for a decimal point
    By face_master in forum C++ Programming
    Replies: 1
    Last Post: 05-14-2002, 12:46 AM
  5. Replies: 3
    Last Post: 11-28-2001, 06:53 AM

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