简体   繁体   English

C ++十六进制控制台输出翻倍需要解决

[英]C++ double to hex console output need help in resolving

my code: 我的代码:

#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;
}

Current output: 100 电流输出: 100

my expected output: x64 我的预期输出: x64

Explanation: I want to print the hex value of decimal part of double. 说明:我要打印double的十进制部分的十六进制值。 But I have been unsuccessful in getting this. 但是我一直没有成功。 need help. 需要帮忙。 Any help in this direction will be appreciated. 对此方向的任何帮助将不胜感激。

What you're asking for is simply not possible. 您所要的完全是不可能的。 std::hex (the output you're looking for) only works for integral arguments, and std::hexfloat uses an undesirable format. std::hex (您要查找的输出)仅适用于整数参数,而std::hexfloat使用不受欢迎的格式。 You need to cast or round. 您需要投射或四舍五入。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM