Thread: Decimal points

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    6

    Unhappy Decimal points

    I'm fairly new to C, and I have an assignment, this is what I came up with so far:

    Code:
     #include<stdio.h>int main()
    {
    float b,e;
    printf("Enter beginning odometer reading=>");
    scanf("%f",&b);
    printf("\nEnter ending odometer reading=>");
    scanf("%f",&e);
    printf("\nYou traveled %.2f miles. At $0.35 per mile, your reimbursement is %.3f.",(e-b),(e-b)*0.35);
    return 0;
    }


    My question is, how can I get rid of the extra decimal point, when I input the following:
    13505.2 (for the beginning odometer reading)
    13810.65 (for the ending odometer reading)

    It gives me values of 305.40 and 106.890, how do I get rid of the extra zeroes?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure it gives you 305.40 and not 305.45?

    At any rate, you told it to print two and three decimal places, respectively, so why are you complaining when it does?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you used %f only in the printf() statement format, I wonder if the last zero would be truncated off the printed number?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adak View Post
    If you used %f only in the printf() statement format, I wonder if the last zero would be truncated off the printed number?
    You're thinking of %g, I think.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    shoot, I made several mistakes, the value for the ending odometer was suppose to be 13810.6. Anyways... thanks for pointing that out, I didn't realise that I stated that. I replaced the values and it works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. aligning decimal points?
    By mp91 in forum C Programming
    Replies: 2
    Last Post: 01-28-2013, 08:30 AM
  2. fwrite and decimal points
    By george7378 in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2011, 06:56 PM
  3. Dealing with decimal points
    By dre in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2009, 02:22 PM
  4. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM
  5. decimal points
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 04-29-2002, 10:01 PM

Tags for this Thread