how to print .03 as 3% in printf?

This is a discussion on how to print .03 as 3% in printf? within the C Programming forums, part of the General Programming Boards category; Thank you....

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    how to print .03 as 3% in printf?

    Thank you.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >how to print .03 as 3% in printf?

    Such like this?

    percentage = 0.03;
    printf ("%d%%", (percentage * 100));

    There is no format specifier which multiplies a number with 100 and puts % after it immediately.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm not sure you can do it directly in printf(), but you could write a function that changes it for you:
    Code:
    void PrintPercent(double DecimalValue)
    {
       int PercentValue = (int)(DecimalValue * 100);
       printf("%d%", PercentValue);
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. printf wont print without newline
    By rupurt in forum C Programming
    Replies: 4
    Last Post: 10-24-2005, 09:05 AM

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