leading zero removal

Probably a dumb question - but here goes anyway. It's been a LONG time since I did any work in C++, and now I find that I need to push some double's to the console with cout... IF the value is less than 1, I need to remove the leading zero.

not -> 0.5
but -> .5

can anyone spare a clue for an old fart?

Thanks,
Ken
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream.h>
#include <sstream.h>


void pause() {
	int i;
	cin >> i;
}


string floatfmt(float f) {
	stringstream oss;
	oss << f;
	string str = oss.str();
	if(str.length() > 2)
		if(str.substr(0, 2) == (string) "0.")
			str = str.substr(1, str.length() - 1);
	return str;
}


void main() {
	float f = 0.12345;
	cout << floatfmt(f) << endl;
	pause();
}


try this ;)
Last edited on
Topic archived. No new replies allowed.