Drop 0 in floating point variable 0.233

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Drop 0 in floating point variable 0.233

 
0
  #1
Feb 18th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Drop 0 in floating point variable 0.233

 
0
  #2
Feb 18th, 2005
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:
  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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Re: Drop 0 in floating point variable 0.233

 
0
  #3
Feb 19th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 7
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Drop 0 in floating point variable 0.233

 
0
  #4
Feb 19th, 2005
For the benifits of others:

You must include <iomanip> to use this manipulator.

  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. }
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC