Can any body help me in converting a double value e.g. 0.000015 to string format in Xcode. so that i may use it as a string. In visual C++ we have to_string function but in Xcode it doesnt works. Regards
Asked
Active
Viewed 2,166 times
0
-
2Use `std::ostringstream`! – πάντα ῥεῖ Jan 14 '14 at 18:18
-
How about this? http://stackoverflow.com/questions/8495076/objective-c-stringvalue-from-double – Dannie Jan 14 '14 at 18:21
-
ostringstream take char value. How ever i want to convert double or long double value to String. – khan Jan 14 '14 at 18:21
-
_'ostringstream take char value'_ No, that's wrong! You can use the output `operator>>()` for any primitive type as usual! – πάντα ῥεῖ Jan 14 '14 at 18:23
-
How can i use this, can you give example forexample my code is given on http://stackoverflow.com/questions/21118842/how-to-convert-long-double-to-string-format-in-xcode-or-how-to-solve-error-us – khan Jan 14 '14 at 18:25
-
How can i fix that, or convert that – khan Jan 14 '14 at 18:26
-
Use the output operator as you would do with `std::cout`, and get the string you want using `std::ostringstream::str()` method. – πάντα ῥεῖ Jan 14 '14 at 18:28
-
when i use std::ostringstream::str(lr_value), it gives error, "call to non-static member function without an object argument" – khan Jan 14 '14 at 18:32
-
See my answer how to use it. (There's also [documentation](http://en.cppreference.com/w/cpp/io/basic_ostringstream) available BTW). – πάντα ῥεῖ Jan 14 '14 at 18:57
2 Answers
4
If you have a compiler supporting C++11, then std::to_string()
is what you want.

jaredready
- 2,398
- 4
- 23
- 50
-
thanks but i used the upper answer, as in this case when i change it to c++11 it give me some other errors...so i used the first one...thanks and regards – khan Jan 15 '14 at 10:47
1
You can make your 'homebrew' to_string()
function very easy using std::stringstream
:
template<typename T>
std::string to_string(T value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}

πάντα ῥεῖ
- 1
- 13
- 116
- 190
-
Thanks alot Bro... It worked, Sorry for late replying...just tried it now... thanks – khan Jan 15 '14 at 10:40