c++ change print format from integer 2255 to $xx.xx - c++

I have function return int i = 2255 which mean how many cents in my pocket and I want to print it in $xx.xx format
how do I print this one to 22.55? Thank you very much

printf("$%d.%d", i / 100, i % 100);
or
printf("$%0.2f", double(i) / 100);
Though, in C++11 and later, consider using std::cout with std::put_money() instead:
#include <iostream>
#include <iomanip>
std::cout << std::put_money(double(i) / 100);

One way is to setup the cout stream to print the format you want:
#include <iostream>
#include <iomanip>
int main()
{
int i = 2250;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << i/100.0 << std::endl; // Divide by 100.00 to convert to double
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

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

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 .

c++ double to string shows floating points

I have a string: (66)
Then I convert it to double and do some math: atof(t.c_str()) / 30
then I convert it back to string: string s = boost::lexical_cast<string>(hizdegerd)
Problem is when I show it on label it becomes 2,20000001.
I've tried everything. sprintf etc.
I want to show only one digit after point.
hizdegerd = atof(t.c_str()) / 30;
char buffer [50];
hizdegerd=sprintf (buffer, "%2.2f",hizdegerd);
if(oncekideger != hizdegerd)
{
txtOyunHiz->SetValue(hizdegerd);
oncekideger = hizdegerd;
}
I think I'd wrap the formatting up into a function template, something like this:
#include <iostream>
#include <sstream>
#include <iomanip>
template <class T>
std::string fmt(T in, int width = 0, int prec = 0) {
std::ostringstream s;
s << std::setw(width) << std::setprecision(prec) << in;
return s.str();
}
int main(){
std::string s = fmt(66.0 / 30.0, 2, 2);
std::cout << s << "\n";
}
You can use this way of conversion back to string and then only the wished number of digits for the precision will be taken in consideration:
ostringstream a;
a.precision(x); // the number of precision digits will be x-1
double b = 1.45612356;
a << b;
std::string s = a.str();
Since you wrote "I want to show":
#include<iostream>
#include<iomanip>
int main()
{
std::cout << std::fixed << std::setprecision(1) << 34.2356457;
}
Output:
34.2
By the way, sprintf is buffer-overflow-vulnerable and is not C++ .

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