Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a C++ Expert!

Join 415,732 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,903 people online right now.Registration is fast and FREE... Join Now!



Convert Double to String Rate Topic: -----

#1 ccubed  Icon User is online

  • Anime. Code. Games.
  • Icon

Reputation: 51
  • View blog
  • Posts: 1,018
  • Joined: 13-June 08


Dream Kudos: 275

Share |

Convert Double to String

Post icon  Posted 03 September 2008 - 10:06 PM

Okay, so I have this code to convert Doubles to Strings.

string stringify(double x){ 
   ostringstream o; 
   o << x; 
   return o.str(); 
}





My question is two fold. One, assume the double was originally 1.2E+003. I convert it to a string and i'll get something like XXXX.XX, but convert it back to a double via istringstream and I get 1.2E+003. Why?

Two, is there any way to get all the decimal numbers from the double and not round when it converts to string? I'm trying to create a nice round function so that I can convert a number that could have 20+ decimals to 2 decimals. The process would be really simple if i could get it into a string form and then just loop through each number behind the decimal and see if it's higher than 5.
Was This Post Helpful? 0
  • +
  • -


#2 RedSonja  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 5
  • View blog
  • Posts: 170
  • Joined: 04-September 08


Dream Kudos: 25

Re: Convert Double to String

Posted 04 September 2008 - 03:09 AM

1. Because it didn't convert it at all, it just formatted it. the number is still there.

2. I don't know the background of what you are up to, but you can format as you like with sprintf and its friends, but they are very complicated to use. When you use these you have exact control of what is happening. If you use the << of streams you cannot be too sure what you will get, esp. if you port to another compiler or hardware.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users