output formatting (aligning decimal point)

This is a discussion on output formatting (aligning decimal point) within the C Programming forums, part of the General Programming Boards category; Can someone tell me what I am mis-understanding or if there is something I need to do? My code is ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    23

    output formatting (aligning decimal point)

    Can someone tell me what I am mis-understanding or if there is something I need to do? My code is this:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    int diameter=1;
    double radius;
    
    printf("enter diameter to calculate radius:\n");
    
    while(diameter > 0)
    {
    scanf("%d",&diameter);
    
    radius = diameter / 2;
    printf("diam\tradius\n");
    printf("\n%4d\t%4.2f\n",diameter, radius);
    }
    return 0;
    }
    say I input 10 then 20

    my output needs to look like this:
    enter diameter to calculate radius:
    10
    diam radius

    10 5.00
    20
    diam radius

    20 10.00

    NOT LIKE THIS:
    enter diameter to calculate radius:
    10
    diam radius

    10 5.00
    20
    diam radius

    20 10.00

    I thought because I did %4.2f that there would be 4 columns total so that the 5.00 would have a blank space before the 5

    Thanks for your help

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    I tried it and it looks all good... whats the problem exactly?

    Code:
    enter diameter to calculate radius:
    10
    diam    radius
    
      10    5.00
    20
    diam    radius
    
      20    10.00

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Ohhh i see what you mean, you want the decimal in the same column? The mistake is that %4 is four characters, not four numbers. Change it to %5.2 and it works, remember that 10.00 is five characters including the decimal point.

    Code:
    enter diameter to calculate radius:
    10
    diam    radius
    
       10    5.00
    20
    diam    radius
    
       20   10.00

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    23
    ahhh thanks you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get a value Before/After a decimal point in C ??
    By ammar555 in forum C Programming
    Replies: 7
    Last Post: 05-18-2011, 02:47 PM
  2. decimal formatting
    By SterlingM in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2009, 01:42 AM
  3. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  4. decimal point allignment
    By Max in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 11:27 PM
  5. Testing for a decimal point
    By face_master in forum C++ Programming
    Replies: 1
    Last Post: 05-14-2002, 01:46 AM

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