problem printing with floating number

This is a discussion on problem printing with floating number within the C++ Programming forums, part of the General Programming Boards category; hello, can any one tell me why the following program is not printing me the zero even though i use ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    181

    problem printing with floating number

    hello, can any one tell me why the following program is not printing me the zero even though i use the setfill function to fill the 0's after the decimal number.

    here is the program:

    this is a funciotn which i am using to print the all the numbers. but when run the program with this value 38.617

    the out put should me this +, integer part -> 38 and the decimal part -> 0.617( here i wanted it as 0.6170)

    but it is not doing not can any one tell why it is
    Attached Files Attached Files
    • File Type: cpp 2.cpp (529 Bytes, 88 views)

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Try adding showpoint and fixed to the output stream. That is:

    std::cout << showpoint << fixed << setprecision(4) << my_float;

    Honestly, I'm not sure what you're trying to do. Can you be more specific?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    basically the problem is to separate the parts of a number like if it is

    38.617

    sign +
    intger part 38
    decimal part 0.6170

    i have written a program to sperate its parts and stored it variables. when i send that variable to the function printout it is not printing the

    decimal part as 0.6170 instead it is printing 0.617 (zero is missing)
    when i tried putting setfill and setprecision function it gives me the same result

    hopefully now you can get the problem what it is

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Maybe this helps?

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
       float d = 0.6740;
    
    
       cout << d << endl;
       cout << showpoint << d << endl;
       cout << setprecision(4) << d << endl;
    
       return 0;
    }
    Output on a Unix machine...

    Code:
    0.674
    0.674000
    0.6740

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    thanks very much guys it is working fine now

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 11:56 AM
  2. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 11:09 AM
  3. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  4. Printing total of 1's in binary number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 01:50 PM
  5. Assingnment problem
    By Jason in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 01:20 PM

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