C++ double to hex console output need help in resolving

标签 c++ cout

我的代码:

#include <iostream>
#include <iomanip> 
using namespace std;

int main() {
    double A = 100.35;
    cout.precision(0);
    cout << std::hexfloat << std::fixed << std::left << A << endl;
    return 0;
}

当前输出: 100

我的预期输出: x64

解释: 我想打印 double 的小数部分的十六进制值。但是我一直没有成功。需要帮忙。在这方面的任何帮助将不胜感激。

最佳答案

您所要求的根本不可能。 std::hex(您正在寻找的输出)仅适用于整型参数,而 std::hexfloat 使用了一种不受欢迎的格式。你需要投或轮。

#include <iostream>
#include <iomanip> 
#include <cmath>
using namespace std;

int main() {
    double A = 100.35;
    cout.precision(0);
    cout << std::hex << std::showbase << std::lround(A) << endl;
    return 0;
}

关于C++ double to hex console output need help in resolving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37414575/

相关文章:

c++ - BST插入、删除、搜索

c++ - 代码意外地给出了无限循环

c# - 从 C# 到非托管 C++ 库中执行 DllImport 时出现 MSBuild 4.0 依赖性问题

C++ 使用 toString() 方法有什么问题

C++ : cout with a terenary if-statement

c++ - 在 cout 语句之间暂停一段时间

c++ - [C++][SFML 2.2] 奇怪的性能问题 - 移动鼠标会降低 CPU 使用率

c++ - 为自定义流类设置 std::io_base 标志

c++ - cout 和 printf 中打印零的区别

c++ - dlib-19.1:从图像(例如 dlib::cv_image)初始化 dlib::matrix 以进行 DNN 训练