1. <legend id='8NIMW'><style id='8NIMW'><dir id='8NIMW'><q id='8NIMW'></q></dir></style></legend>
      • <bdo id='8NIMW'></bdo><ul id='8NIMW'></ul>
    2. <tfoot id='8NIMW'></tfoot>

        <i id='8NIMW'><tr id='8NIMW'><dt id='8NIMW'><q id='8NIMW'><span id='8NIMW'><b id='8NIMW'><form id='8NIMW'><ins id='8NIMW'></ins><ul id='8NIMW'></ul><sub id='8NIMW'></sub></form><legend id='8NIMW'></legend><bdo id='8NIMW'><pre id='8NIMW'><center id='8NIMW'></center></pre></bdo></b><th id='8NIMW'></th></span></q></dt></tr></i><div id='8NIMW'><tfoot id='8NIMW'></tfoot><dl id='8NIMW'><fieldset id='8NIMW'></fieldset></dl></div>

        <small id='8NIMW'></small><noframes id='8NIMW'>

        在 C++ 中使用 boost::lexical_cast 将双精度转换为字符串?

        Convert double to string using boost::lexical_cast in C++?(在 C++ 中使用 boost::lexical_cast 将双精度转换为字符串?)

        <small id='SWU8I'></small><noframes id='SWU8I'>

        <i id='SWU8I'><tr id='SWU8I'><dt id='SWU8I'><q id='SWU8I'><span id='SWU8I'><b id='SWU8I'><form id='SWU8I'><ins id='SWU8I'></ins><ul id='SWU8I'></ul><sub id='SWU8I'></sub></form><legend id='SWU8I'></legend><bdo id='SWU8I'><pre id='SWU8I'><center id='SWU8I'></center></pre></bdo></b><th id='SWU8I'></th></span></q></dt></tr></i><div id='SWU8I'><tfoot id='SWU8I'></tfoot><dl id='SWU8I'><fieldset id='SWU8I'></fieldset></dl></div>

            <tbody id='SWU8I'></tbody>
        • <legend id='SWU8I'><style id='SWU8I'><dir id='SWU8I'><q id='SWU8I'></q></dir></style></legend>
            <bdo id='SWU8I'></bdo><ul id='SWU8I'></ul>

                <tfoot id='SWU8I'></tfoot>
                  本文介绍了在 C++ 中使用 boost::lexical_cast 将双精度转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 lexical_cast 将浮点数转换为字符串.通常它工作正常,但我对没有小数的数字有一些问题.如何修复字符串中显示的小数位数?

                  I' d like to use lexical_cast to convert a float to a string. Usually it works fine, but I have some problems with numbers without decimal. How can I fix number of decimal shown in the string?

                  示例:

                  double n=5;
                  string number;
                  number = boost::lexical_cast<string>(n);
                  

                  结果编号是5,我需要编号5.00.

                  Result number is 5, I need number 5.00.

                  推荐答案

                  来自 提升 lexical_cast:

                  对于更复杂的转换,例如需要比 lexical_cast 的默认行为提供的更严格控制的精度或格式,建议使用传统的 stringstream 方法.在数字到数字的转换中,numeric_cast 可能提供比 lexical_cast 更合理的行为.

                  For more involved conversions, such as where precision or formatting need tighter control than is offered by the default behavior of lexical_cast, the conventional stringstream approach is recommended. Where the conversions are numeric to numeric, numeric_cast may offer more reasonable behavior than lexical_cast.

                  示例:

                  #include <sstream>
                  #include <iomanip>
                  
                  int main() {
                      std::ostringstream ss;
                      double x = 5;
                      ss << std::fixed << std::setprecision(2);
                      ss << x;
                      std::string s = ss.str();
                      return 0;
                  }
                  

                  这篇关于在 C++ 中使用 boost::lexical_cast 将双精度转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                  <legend id='nqtSV'><style id='nqtSV'><dir id='nqtSV'><q id='nqtSV'></q></dir></style></legend>

                        • <bdo id='nqtSV'></bdo><ul id='nqtSV'></ul>
                            <tbody id='nqtSV'></tbody>
                          <tfoot id='nqtSV'></tfoot>
                          • <small id='nqtSV'></small><noframes id='nqtSV'>

                            <i id='nqtSV'><tr id='nqtSV'><dt id='nqtSV'><q id='nqtSV'><span id='nqtSV'><b id='nqtSV'><form id='nqtSV'><ins id='nqtSV'></ins><ul id='nqtSV'></ul><sub id='nqtSV'></sub></form><legend id='nqtSV'></legend><bdo id='nqtSV'><pre id='nqtSV'><center id='nqtSV'></center></pre></bdo></b><th id='nqtSV'></th></span></q></dt></tr></i><div id='nqtSV'><tfoot id='nqtSV'></tfoot><dl id='nqtSV'><fieldset id='nqtSV'></fieldset></dl></div>