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

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
my expected output:
x64
Explanation:
I want to print the hex value of decimal part of 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. 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;
}

Related

How to cout int like a double

I'd like to display some integer with currency index (I mean dot and double zero)
For example like here:
#include <iostream>
int main() {
int w1=700,c1=99,c2=98;
double noh2o=w1*(100.0-c1)/100.0;
double w2=noh2o+noh2o/(100.0-c2)*c2;
std::cout<<w2<<std::endl;
}
If somebody can help me I will be thankful.
You are supposed to do it with the locale library.
Mostly copied from https://en.cppreference.com/w/cpp/io/manip/put_money like so:
#include <iomanip>
#include <iostream>
#include <locale>
int main() {
long double val = 239.9;
std::cout.imbue(std::locale("en_US.UTF-8"));
std::cout << std::showbase
<< "en_US: " << std::put_money(val)
<< std::endl;
return 0;
}
Use std::fixed and std::setprecision.
Try it online!
#include <iostream>
#include <iomanip>
int main() {
int w1=700,c1=99,c2=98;
double noh2o=w1*(100.0-c1)/100.0;
double w2=noh2o+noh2o/(100.0-c2)*c2;
std::cout << std::fixed << std::setprecision(2) << w2 << std::endl;
}
Output:
350.00

How to print the fractional part of float number while converting to string

I have a code which will convert the float value to string, i have written like below
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
return 0;
}
But the problem is when my value is 10.66 its coming 10.66 but when its 10.80 its coming like 10.8 or when its 10.00 its coming 10 only .
How can i print the complete value
Try this code .
Use the setprecision function with '2' .
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
float myFloat= 10.80;
stringstream stream;
stream << fixed << setprecision(2) << myFloat;
string s = stream.str();
cout<<"value = " << s;
return 0;
}
The trailing zeros are only kept if you set either fixed or scientific mode.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 4.2;
cout << fixed << setprecision(2);
cout << x << endl;
return 0;
}
It seems you want something like below.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << fixed << setprecision(2) << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
}
Probably the least complicated way would be to use printf instead of std::cout. There you can specifically specify how many digits are to be displayed.
#include "stdio.h"
printf("%3.2f",myfloat);
where 3 is the # of digits before and 2 the # of digits after the dot, either can be left out. Append '\n' to the string if you want a new line.
EDIT: Ok, I did not know about setprecision(2).

Loss of Precision when Converting Double to String in C++

So I know setprecision(int n) should be used when printing a double value with precision n. However, I've run into a problem on a project that I'm working on that is akin to this code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double var = 1.0000001;
cout << setprecision(10)<< var << endl;
string str = to_string(var);
cout << str << endl;
return 0;
}
Here is the output:
1.0000001
1.000000
In the project I'm working on, I need to save the double value as a string, and it will occasionally need more than six decimal places of precision. Here, precision is clearly lost in the conversion. Any pointers would be greatly appreciated.
You can use std::stringstream.
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main(void) {
double var = 1.0000001;
cout << setprecision(10)<< var << endl;
stringstream ss;
ss << setprecision(10) << var;
string str;
ss >> str;
cout << str << endl;
return 0;
}
If you want to get the full precision of your double without limiting it to a specific value (implied by your "occasionally need more than six decimal places"), and if you are using the Boost libraries, you can also try this following alternative:
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace boost;
using namespace std;
int main() {
double var = 1.0000001;
cout << lexical_cast<string>(var) << endl;
return 0;
}
This has proven useful in one of my applications where precision did matter, and where the std::stringstream approach was not very convenient and elegant due to usage of some specific logging function wrappers. You can find more information about boost::lexical_cast and how it deals with internal representations here.
Obviously, if you are not currently using Boost in your project, this approach is overkill.
try this:
#include <sstream>
#include <string>
// In some function:
double d = 453.23;
std::ostringstream os;
os << d;
std::string str = os.str();

How can I output the result of atof as 1.0 instead of 1

I have a problem using atof,
here is the code:
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(){
std::string num ("1.0");
//std::string num ("1.1");
cout<< atof(num.c_str());
return 0;
}
If the num string is "1.1" , it can correctly cout 1.1. But if I want to keep the zero when the num string is "1.0" (want it to be 1.0 but not 1), what should I do?
You need to use std::fixed and std::setprecision, like so:
std::cout<< std::fixed << std::setprecision(1) << atof(num.c_str());
This will require that you include the iomanip header.
A possible solution is
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::cout.precision(3);
std::cout.setf(std::ios::fixed);
std::string s("1.0");
float f = 0.0f;
sscanf(s.c_str(), "%f", &f);
// alternative way of setting this flags
// std::cout << std::fixed << std::setprecision(3) << f << "\n";
std::cout << f << "\n";
return (0);
}
notice that there are at least 2 ways of accomplishing the same format for the output, I left one of them commented out .

String manipulation, when converted show 0 in the beginning of the string

I want to know how can I make the string I converted from DWORD to onstringstream and then to AnsiString.
But that doesn't really matter, the conversion could be from int to string, I just want to know how I can make every string converted to ALWAYS show 6 digits, like if my number is 57, in the string it will be 000057.
Thanks!
Use io manipulators setfill and setw:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main()
{
std::ostringstream s;
s << std::setfill('0') << std::setw(6) << 154;
std::cout << s.str() << "\n";
return 0;
}
So, the question about formatted output?
you can use iostream::width and `iostream::fill':
// field width
#include <iostream>
using namespace std;
int main () {
cout << 100 << endl;
cout.width(6);
cout.fill('0');
cout << 100 << endl;
return 0;
}