944,132 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2519
  • C++ RSS
Feb 18th, 2005
0

Drop 0 in floating point variable 0.233

Expand Post »
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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dallin is offline Offline
14 posts
since Jan 2005
Feb 18th, 2005
0

Re: Drop 0 in floating point variable 0.233

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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. double f = 0.123;
  9. ostringstream sout;
  10.  
  11. sout<< f;
  12. string s = sout.str();
  13. cout<< s.substr(1, s.length()) <<endl;
  14. }
That's hardly worth the effort for something so small as removing a 0 for no apparent benefit.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 19th, 2005
0

Re: Drop 0 in floating point variable 0.233

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!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dallin is offline Offline
14 posts
since Jan 2005
Feb 19th, 2005
0

Re: Drop 0 in floating point variable 0.233

For the benifits of others:

You must include <iomanip> to use this manipulator.

C++ Syntax (Toggle Plain Text)
  1.  
  2. // setprecision example
  3. #include <iostream>
  4. #include <iomanip>
  5. using namespace std;
  6. int main () {
  7. double f =3.14159; cout << setprecision (5) << f << endl;
  8. cout << setprecision (9) << f << endl;
  9. return 0;
  10. }
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Programming Class Help!
Next Thread in C++ Forum Timeline: I need a solution for a opengl thing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC