Thread: decimal??

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    decimal??

    Hi there i don't get this using cout and printf here's the code

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <iomanip>
    #include <conio.h>
    
    using namespace std;
    
    int main(void)
    {
        float num;
        
        printf("input number: \n");
        scanf("%f", &num);
        printf("The number is: %.2f\n", num);
        cout << setprecision (4) << "The number is: " << num << endl;
        getch();
    
        
    }
    why when using printf you can easily change the decimal places but why in cout??? for example i input number 11.50 the output goes like this.
    Code:
    The number is 11.50 <--------------------------- using printf.
    The number is 11.5    <--------------------------using cout.
    why is that? cout can't print the last zero even if i use <iomanip>?
    any idea there? thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    guess what you want is
    Code:
        printf("The number is: %.2f\n", num);
        cout <<"The number is: " << setprecision(2) << fixed <<  num << endl;
    Kurt

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Thank you very much mate, i really appreciate your help....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 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. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM