Thread: lining up decimals

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    14

    lining up decimals

    I need to line up my input. I am taking type double as my input how could I get the decimalsin line when I output them?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    14

    precision

    One more thing, is there another type that I can use besides double to get a greater precision(more decimal points)?

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how could I get the decimalsin line when I output them?
    do you mean printf format modifiers?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    I know how to use printf but for example say you input vaules as
    32.5
    2.3
    125.6
    1110.3

    I need to output them in the following format:

    32.5
    2.3
    125.6
    1110.3

    where the decimals line up with one another.

    Thanks

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use the code tags to keep the formating on these boards.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    printf("Enter a number...");
    scanf("%lf" , &n);
    [/code]


    Need to out put as follows:
    32.5
    2.3
    125.6
    1110.3

    Is that what you mean?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do this
    [code]
    Need to out put as follows:
    32.5
    2.3
    125.6
    1110.3
    [/code]
    And you'll get
    Code:
    Need to out put as follows:
    32.5
    2.3
    125.6
    1110.3
    ... so you can format the output correctly:
    Code:
    Need to out put as follows:
       32.5
        2.3
      125.6
     1110.3
    Do this, and tell us what you want again.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    I need to line up the input by their decimals.
    Code:
    input as follows:
    32.5
    2.3
    125.6
    1110.3
    Code:
    Need to output as follows:
      32.5
       2.3
     125.6
    1110.3
    Last edited by jwhitaker3; 09-09-2002 at 08:47 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try
    printf("%8.1f\n", d);

    > is there another type that I can use besides double to get a greater precision
    There's long double, but it only adds a few decimal places

    If you want more, then you need to resort to an external math package like
    GMP
    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.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    printf("%8.1f", n)

    Only lets one decimal place be output.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    Yes I have tried 8.3f and different values.
    This only lines up the first digit

    12.3
    2.3

    However I need it to be this.
    Code:
    12.3
     2.3

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf (">%8.3f<\n", 12.3);
        printf (">%8.3f<\n", 2.3);
        return 0;
    }
    
    /*
    Program output:
    
    >  12.300<
    >   2.300<
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating very long decimals
    By scwizzo in forum C++ Programming
    Replies: 9
    Last Post: 08-25-2008, 01:57 AM
  2. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 04:13 PM
  3. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  4. Lining up decimals & text - code included inside.
    By INFERNO2K in forum C Programming
    Replies: 7
    Last Post: 11-27-2004, 04:49 PM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM