keeping zero after floating point

Hi all,
Let's say I have:

1
2
3
double hour = 3.00;

cout << 1.20 + 3.00;


It prints 4.2 , but I want 4.20 (with a zero after 2). How can this be done ? I need this format to loop through clock hours, or maybe there is a special type for this, like a time or hour type ? Any suggestions ?
Thanks in advance for any help.

P.S: Sorry for my english.
I'm not sure there's a workaround to that. I think it automatically rounds 4.20 to 4.2 because the zero really isn't necessary. =/
There is a workaround: convert to a string, search for dot character, get the size and append manually zeroes as needed.
The trailing zeros are only kept if you set either fixed or scientific mode. The default floating point formatting mode (if you don't explicitly set one) does not keep the trailing zeros.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double x  = 4.2;
    double dx = 0.25;

    cout << fixed << setprecision(2);
	
    for(int i = 0; 5 > i; ++i)
    {
        cout << x << endl;
        x += dx;
    }

    return 0;
}


4.20
4.45
4.70
4.95
5.20


See http://www.cplusplus.com/reference/iostream/ios_base/precision/ for more details.

Andy
Last edited on
@modoran I disagree with the advice. There is a much easier and more direct way to do it: use manipulators.
For example using

1
2
3
f = 1.2;
cout << f;
cout << left << setfill('0') << setw(4) << f << "\n";

displays
1.2
1.20


You need to include <iomanip> for this also.

To put it shortly you define that at least 4 characters will be present (setw(4)), set the fill character (the character used when less character are available) to be '0' (setfill('0')) and set the place where to insert these characters (left). So you have some of the functionality of the old printf supported

see
http://www.cplusplus.com/reference/iostream/manipulators/setw/
http://www.cplusplus.com/reference/iostream/manipulators/setfill/
http://www.cplusplus.com/reference/iostream/manipulators/left/
for more information over this
I think I only half read the original question: the keeping the trailing zeroes of an outputted double. But not the time part.

There is time_t, but that's just a integral type (usually unsigned long long these days) which holds a count of seconds since 1 Jan 1970, and will display just like a regular integer. And there's struct tm, which you'd have to display member by member. Plus various system specific types.

But there's no standard date or time class in C++.

Boost does have the boost::date class, which might be of interest?

Failing that, you could code your own time or date class.

You do need a time class if you want to code an insertion operator which know how to format time properly!

If I was coding one for prototyping purposes, I'd probably base the class on time_t so I can use the standard time routines to do the work for me. e.g. strftime()
http://www.cplusplus.com/reference/clibrary/ctime/strftime/

Andy
Last edited on
Topic archived. No new replies allowed.