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 could try a more compact style:
The boost (tm) way:
The Standard C++ way:
Note: Don't forget
#include <sstream>
The Standard C++11 way (if you don't care about the output format):
to_string
is a new library function introduced in N1803 (r0), N1982 (r1) and N2408 (r2) "Simple Numeric Access". There are also thestod
function to perform the reverse operation.If you do want to have a different output format than
"%f"
, use thesnprintf
orostringstream
methods as illustrated in other answers.I would look at the C++ String Toolkit Libary. Just posted a similar answer elsewhere. I have found it very fast and reliable.
Herb Sutter has an excellent article on string formatting. I recommend reading it. I've linked it before on SO.
Normaly for this operations you have to use the ecvt, fcvt or gcvt Functions:
As a Function: