Thread: Problem Formatting double numbers.

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

    Problem Formatting double numbers.

    Hi all,

    I have problem formatting double number. When double number is stored in computer(binary representation), it is not exact as decimal representation. when I try to format the double number with more decimal points(ex- 12) then as number gets bigger, I get more and more noise which makes number look different than the one entered as decimal number. How can I find the way to format the number so I do not see noise? or How can I find out how many decimal points I can safely show without getting noise for any double number? thanks...

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    double is a floating point data type. What this means is that the decimal point is not fixed at some position, but moves as necessary. If you're having problems aligning numbers on the screen, try something like this:
    Code:
    #include <cstdio>
    
    int main()
    {
        double d[] = { 5.5, -12.599, 124.42112 };
        int i;
    
        // Loop through all elements of the array
        for(i = 0; i < sizeof(d) / sizeof(d[0]); i++)
        // %9.5f specifies that a double is being passed,
        // which should be formatted to be at least 9
        // characters long with 5 characters after the
        // after the decimal point.
            printf("%9.5f\n", d[i]);
    
        // Output is:
        //   5.50000
        // -12.59900
        // 124.42112
    
        return 0;
    }
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. Little Problem on Presentation in the Tower of Hanoi
    By momegao in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2003, 06:22 PM
  5. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 09:54 PM