I'm using visual studio .net c++ and I'm looking for the easiest code to drop the 0 on 0.233 to .233 when displaying floating point variable on screen. Thanks.

12 Years
Discussion Span
Last Post by BountyX

My first question is: why bother? Unless there's a trick that's slipped my mind, you need to copy the value into a string and then remove the first character:

#include <iostream>
#include <sstream>

using namespace std;

int main()
  double f = 0.123;
  ostringstream sout;

  sout<< f;
  string s = sout.str();
  cout<< s.substr(1, s.length()) <<endl;

That's hardly worth the effort for something so small as removing a 0 for no apparent benefit.


I agree, however, I believe its for my educational benefit only and my grade. I've tried it and like it, however, what additional code do I need to limit the digits displayed to 3? Found it, I think I'm taking care of. Thanks!


For the benifits of others:

You must include <iomanip> to use this manipulator.

// setprecision example
#include <iostream>
#include <iomanip>
using namespace std;
int main () { 
double f =3.14159; cout << setprecision (5) << f << endl; 
cout << setprecision (9) << f << endl; 
return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.