Thread: format number

  1. #1
    Unregistered
    Guest

    format number

    hi everyone,
    i have a simple question because i'm a newbie in c programming. i need to format a double to a currency like number : something like 11.00. How can I do this?

    also, i'm trying to print a double to the screen but it's not displaying correctly. here is my code:

    printf ("sInt = %lf\n", &sInt); //this always display 0.0000000 instead of the correct number.

    Thanks a lot

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you trying to print it's address as a float? Do not use & when using printf.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, in the function printf, you pass by value, not reference (&). You need not to change the variable just work with the value.
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Wink

    This would be one of the first things in just about all tutorials and most books that you would learn. Check out a few tutorial sites, It really helps.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's an example of rounding to two decimal places.

    double d = 1.23456789;
    //either
    printf("%.2lf\n",d);
    //or this
    printf("%5.2lf\n",d);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM