Display numbers with padding and a fixed number of digits in C++ - c++

I'd like to display numbers using a padding (if necessary) and a fixed number of digits. For instance, given the following numbers:
48.3
0.3485
5.2
Display them like this:
48.30
00.35
05.20
I'm trying combinations of std::fixed, std::fill, std::setw, and std::setprecision, but I can't seem to get what I'm looking for. Would love some guidance!
NOTE: The 0-padding isn't really critical, but I'd still like the numbers to be aligned such that the decimal point is in the same column.

It's pretty straightforward
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << setprecision(2) << setfill('0');
cout << setw(5) << 48.3 << endl;
cout << setw(5) << 0.3485 << endl;
cout << setw(5) << 5.2 << endl;
}
Writing code like this makes me yearn for printf however.

Related

How to ouput an integer with positive sign and zeros preceding it

I want a number to be displayed with a positive sign and three 0's preceding it, but what I am getting so far is 000+1 when what I want is +0001
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
int number = 1;
cout << showpos;
cout << setfill('0') << setw(5) << number << endl;
}
You need to also set std::internal flag. This way you will get your expected +0001 - test at ideone.
This is what the std::internal manipulator is for. For example,
std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << std::endl;
prints "-0005" instead of "000-5" as without std::internal.

Trouble with alignment using iomanip

I'm not great at using the iomanip library and have tried looking around for similar problems, but haven't been able to implement a good solution. I'm trying to display words that are used most frequently and least frequently. My code is (abridged version showing what I'm having problems with):
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string word1 = "helloooo";
string word2 = "hey";
cout << "MOST FREQUENT: ";
cout << setw(7) << left << word1;
cout << setw(5) << right << "(" << 5 << ")" << endl;
cout << "LEAST FREQUENT: ";
cout << setw(9) << left << word2;
cout << setw(5) << right << "(" << 3 << ")" << endl;
}
This is only a simpler version of the code I'm working on. In reality, the words will be pulled from a vector and the numbers will be different. The words should be center aligned and the right side should show the number in a column. I want it to look like:
MOST FREQUENT: hellooooo (5)
LEAST FREQUENT: hey (3)
But instead I get:
MOST FREQUENT: helloooo (5)
LEAST FREQUENT: hey (3)
I'm just not good using iomanip and any help would be appreciated!

Print a decimal number in a particular format using setfill() in c++

I am trying to print a decimal no. in following format : "#####+3.01"
Case: There is a decimal no (let say 3.01 in this case). I have to print it with its sign +/- preceding with y no. of #, with some fix total width. (let say x = 10 in this case).
I tried do something like this :
double no = 3.01;
cout << setfill('#') << setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
But i am getting followinfg output :
+#####3.01
Expected Output :
#####+3.01
Your code gave me correct result. I am using a Linux machine.
Just in case it is a OS dependent problem, try this code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double no = 3.01;
cout << setfill('#') << std::right<< setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
}

How to set the output precision to 2 decimal places in C++?

I want to globally set the ouptut precision to 2 decimal places.
I already tried to use iomanip and setprecision, however I keep getting output with 'e' in it.
This is my example code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double pay=16.78;
double hours;
double total;
cout.precision(2);
cout << "Please enter how many hours you worked : " << endl;
cin >> hours;
total=hours*pay;
cout << "You earned: " << total << endl;
}
You can use something like this:
double pay = 393.2993;
std::cout << std::fixed << std::setprecision(2) << pay;
You will need to include iomanip for this to work.
#include <iomanip>
I'm not familiar with "cout.precision(2)". I would use std::setprecision(4) if I wanted my output to have 4 significant figures.
If your e is a positive value, you cannot ride of them because your value is too large. This code
std::cout << std::setprecision(3) << 3e45;
//output
3e+45
If it's a negative number, your precision is not enough. Like the following code
std::cout << std::setprecision(3) << 3e-45; //
//output
3e-45
A simple way will be to use std::fixed
std::cout << std::fixed << std::setprecision(3) << 3.63e-2;
//output
0.036
But the disavantage you have with the std::fixed is it shows the zero after the last none-zero number until it reach the setprecision qunatity you set previously.

function return ostream with precision [duplicate]

This question already has answers here:
Printing the correct number of decimal points with cout
(13 answers)
Closed 6 years ago.
Similar topic is already discussed in the forum. But I have some different problem in following code:
double total;
cin >> total;
cout << fixed << setprecision(2) << total;
If I give input as 100.00 then program prints just 100 but not 100.00
How can I print 100.00?
cout << fixed << setprecision(2) << total;
setprecision specifies the minimum precision. So
cout << setprecision (2) << 1.2;
will print 1.2
fixed says that there will be a fixed number of decimal digits after the decimal point
cout << setprecision (2) << fixed << 1.2;
will print 1.20
It is possible to print a 15 decimal number in C++ using the following:
#include <iomanip>
#include <iostream>
cout << fixed << setprecision(15) << " The Real_Pi is: " << real_pi << endl;
cout << fixed << setprecision(15) << " My Result_Pi is: " << my_pi << endl;
cout << fixed << setprecision(15) << " Processing error is: " << Error_of_Computing << endl;
cout << fixed << setprecision(15) << " Processing time is: " << End_Time-Start_Time << endl;
_getch();
return 0;
The easiest way to do this, is using cstdio's printf. Actually, i'm surprised that anyone mentioned printf! anyway, you need to include the library, like this...
#include<cstdio>
int main() {
double total;
cin>>total;
printf("%.2f\n", total);
}
This will print the value of "total" (that's what %, and then ,total does) with 2 floating points (that's what .2f does). And the \n at the end, is just the end of line, and this works with UVa's judge online compiler options, that is:
g++ -lm -lcrypt -O2 -pipe -DONLINE_JUDGE filename.cpp
the code you are trying to run will not run with this compiler options...
Using header file stdio.h you can easily do it as usual like c. before using %.2lf(set a specific number after % specifier.) using printf().
It simply printf specific digits after decimal point.
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
double total=100;
printf("%.2lf",total);//this prints 100.00 like as C
}
This will be possible with setiosflags(ios::showpoint).