Question about decimal places

This is a discussion on Question about decimal places within the C++ Programming forums, part of the General Programming Boards category; I am needing to output dollar amounts to the screen. I tried using float and double but they only print ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    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
    ZuK
    ZuK is offline
    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, 01:14 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 04: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, 08:52 AM
  5. Replies: 10
    Last Post: 06-12-2002, 04:15 PM

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