954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Drop 0 in floating point variable 0.233

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.

dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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!

dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

For the benifits of others:

You must include 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;
}
BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You