Thread: Question about decimal places

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Question about decimal places

    I am needing to output dollar amounts to the screen. I tried using float and double but they only print numbers that are greater than 0. I use a function to do a calculation and when I return the calculated value it only prints non-zero numbers. For example, when the calculation is done and to total amount should be $3.50 it only prints 3.5 How do I get it to print 3.50?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    #include <iomanip> // stream manipulators for formatted output
    #include <iostream>
    
    int main(void) {
        double dollars = 3.5;
        cout << "$" << std::setprecision(2) << dollars <<"\n";
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You should use fixed as well
    Code:
        std::cout << "$" << std::fixed << std::setprecision(2) << dollars <<"\n";
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM