Thread: big numbers

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    big numbers

    how do i display a floating point number without the decimals.

    I.E., how do i make this:

    //code:
    printf("%f", number);

    //output:
    1.00000



    look like this:

    //code
    printf("%f", number);

    //output
    1
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    display it as a int not a float.
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    no no.... you dont understand...... maybe i should make it more clear:

    how do i turn this:

    //code
    printf("%f", number);
    //output
    99999999999999999999999999999.00000


    into this:

    //code
    printf("%f", number);
    //output
    99999999999999999999999999999
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    printf("/i",number) ;
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    doesnt even work at all....

    prints /i

    or if you meant \i, it gives an 'unknown escape sequance' error.
    I came up with a cool phrase to put down here, but i forgot it...

  6. #6
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Code:
    float number = 1234.56;
    
    printf( "%f", (int)number );
    casting to int will truncate the fractional part.
    Last edited by taylorguitarman; 03-14-2002 at 10:32 PM.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    that doesnt work either.

    now it just prints the number as ' 0.00000 ' no matter what.
    I came up with a cool phrase to put down here, but i forgot it...

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    printf("%0.0f", f);

    ;-)

  9. #9
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Sorry, I meant this:

    printf( "%d", (int)number );
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    Well, you could just use:

    printf("%.0f",f);

    The .0 means 0 digits printed after the decimal point. .1 would mean 1 digit etc.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    ahh...


    beautiful!
    I came up with a cool phrase to put down here, but i forgot it...

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    202

    my mistake

    I really should stop visiting this board once I can't remeber the last time I slept(I think it was last tuesday :-)).
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    For God's sake:

    printf("%d",number);
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Totally useless question

    Why is it %d for integers, instead of %i or something?????

    PS: I'm ALWAYS fooled that % =96
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why is it %d for integers, instead of %i or something?????
    Both are allowed, but there are differences in how they behave and should be used.

    For printf, %d and %i are both flags for signed decimal notation. For scanf, %d is a decimal integer pointer and %i is an integer pointer which may be of a base other than decimal.

    %i isn't used often because it's better to be explicit in how you are representing the base with %o, %x, and %d.

    Btw,
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      long double d = 12345678987634534.0;
      printf ( "%.0lf\n", d );
      return 0;
    }
    Fiddle with this a bit, you'll find that you can remove the decimal and anything that follows with a %.0 flag, and a long double will give you a precision up to 17 digits.

    >printf( "%d", (int)number );
    Have you tried this? It works okay as long as the number is small enough to fit in an integer, otherwise you get incorrect output.

    >printf("%d",number);
    No, and test something before you offer it as a solution.

    -Prelude
    Last edited by Prelude; 03-15-2002 at 04:46 PM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Problem with adding up numbers from a txt file
    By Leo12 in forum C Programming
    Replies: 1
    Last Post: 02-18-2009, 01:50 PM
  4. Big (and small) numbers...
    By swif in forum C Programming
    Replies: 6
    Last Post: 04-22-2005, 12:21 PM
  5. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM