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.

Recommended Answers

All 3 Replies

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.