gpt4 book ai didi

c++ - wstringstream 默认格式标志

转载 作者:行者123 更新时间:2023-11-28 07:29:00 27 4
gpt4 key购买 nike

我正在尝试将 3 字节 double 型转换为字符串。以下是我的代码。

    double b = 0xFFFFFF;    
std::wstring ss;
std::wstringstream sOut;

sOut << b;
ss = boost::lexical_cast<std::wstring>(sOut.str());

我预计输出为 16777215。但是“ss”的值为 1.67772e+007。

但是,当我使用“固定”标志时,我得到了预期的输出。

sOut << std::fixed

我的问题是 wstringstream 是否默认具有“科学”标志?

谢谢,

最佳答案

所有流(不仅是 wstringstream)默认情况下都将浮点格式化标志设置为 ios_base::defaultfloat,这会请求您正在观察的格式,即相当于 printf 的转换说明符 %g

引用C对%g的描述

A double argument representing a floating-point number is converted in style f or e (or in style F or E in the case of a G conversion specifier), depending on the value converted and the precision. Let P equal the precision if nonzero, 6 if the precision is omitted, or 1 if the precision is zero. Then, if a conversion with style E would have an exponent of X:

  • if P > X >= -4, the conversion is with style f (or F) and precision P - (X + 1).
  • otherwise, the conversion is with style e (or E) and precision P - 1.

在您的例子中,选择了“样式 e”。

关于c++ - wstringstream 默认格式标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18089946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com