I need to store a double as a string. I know I can use printf
if I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the key).
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Do the Java Integer and Double objects have unnece
- Why does const allow implicit conversion of refere
- how to split a list into a given number of sub-lis
相关文章
- 关于C#中 float、double、decimal 的运算不精确的问题。
- JSP String formatting Truncate
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
You can convert any thing to anything using this function:
usage :
You can use std::to_string in C++11
If you use C++, avoid
sprintf
. It's un-C++y and has several problems. Stringstreams are the method of choice, preferably encapsulated as in Boost.LexicalCast which can be done quite easily:Usage:
You may want to read my prior posting on SO. (Macro'ed version with a temporary ostringstream object.)
For the record: In my own code, I favor snprintf(). With a char array on the local stack, it's not that inefficient. (Well, maybe if you exceeded the array size and looped to do it twice...)
(I've also wrapped it via vsnprintf(). But that costs me some type checking. Yelp if you want the code...)
Use
to_string()
.example :
run it yourself : http://ideone.com/7ejfaU
These are available as well :