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++)
The problem with lexical_cast is the inability to define precision. Normally if you are converting a double to a string, it is because you want to print it out. If the precision is too much or too little, it would affect your output.
Heh, I just wrote this (unrelated to this question):
You could also use stringstream.
Take a look at
sprintf()
and family.sprintf
is okay, but in C++, the better, safer, and also slightly slower way of doing the conversion is withstringstream
:You can also use Boost.LexicalCast:
In both instances,
str
should be"453.23"
afterward. LexicalCast has some advantages in that it ensures the transformation is complete. It usesstringstream
s internally.