Thread: Dealing with decimal points

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    20

    Dealing with decimal points

    I'm printing out a bunch of numbers with a for loop which makes up a table. Numbers such as 526.74 or 921.63 contribute to the alignment of the table since there are a total of 5 digits in the number. I also have the number 1005 being printed, but since its a whole number does not print any decimal places and ruins the alignment of that row. For example

    526.74 IS A NUMBER
    921.63 IS A NUMBER
    457.43 IS A NUMBER
    1005 IS A NUMBER

    Basically I need it to print out 1005.00 so that everything is aligned, is there anyway I can get it to print 1005.00 instead of 1005, also since its part of a for loop making any changes will effect all numbers and not just 1005.

    Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you want things lined up, then you should probably use some combination of the setw, setprecision and fixed stream manipulators.

    Code:
    #include <iomanip>
    
    ...
    
    std::cout << std::fixed << std::setw(7) << std::setprecision(2) << your_value_here << std::endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    hey dude that worked thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  2. Yahtzee C++ programme help
    By kenneth_888 in forum C++ Programming
    Replies: 13
    Last Post: 09-05-2007, 02:14 PM
  3. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 04:30 PM
  4. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM
  5. decimal points
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 04-29-2002, 10:01 PM